Skip to content

Instantly share code, notes, and snippets.

@cziem
Created September 21, 2019 09:47
Show Gist options
  • Save cziem/b634fb93953fd69bb70e00a90d60daf2 to your computer and use it in GitHub Desktop.
Save cziem/b634fb93953fd69bb70e00a90d60daf2 to your computer and use it in GitHub Desktop.
Setup the an express server
const express = require("express");
const mongoose = require("mongoose");
const port = process.env.PORT || 4000;
const uri = process.env.MONGODB_URI;
const options = {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
};
mongoose
.connect(uri, options)
.then(() => console.log("Connected to the DB"))
.catch(err => {
console.log(`Could not connect to DB: ${err}`);
process.exit(1);
});
const app = express();
//create a server object:
app.listen(port, err => {
if (err) {
console.log(`Could not start the server: ${err}`);
process.exit(1);
}
console.log(`server started at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment