Created
September 21, 2019 09:47
-
-
Save cziem/b634fb93953fd69bb70e00a90d60daf2 to your computer and use it in GitHub Desktop.
Setup the an express server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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