Created
October 7, 2019 18:09
-
-
Save artemis15/c2060f82f2c664f9f3897c39efca6700 to your computer and use it in GitHub Desktop.
App.js with JWT
This file contains 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 bodyParser = require("body-parser"); | |
const expressValidator = require('express-validator'); | |
let routes = require("../routes/routes"); | |
let server = express(); | |
let create = (config, db) => { | |
server.set("env", config.env); | |
server.set("port", config.port); | |
server.set("hostname", config.hostname); | |
// add middleware to parse the json | |
server.use(bodyParser.json()); | |
server.use(expressValidator()); | |
server.use( | |
bodyParser.urlencoded({ | |
extended: false | |
}) | |
); | |
//connect the database | |
mongoose.connect(db.database, { | |
useNewUrlParser: true, | |
useCreateIndex: true, | |
useUnifiedTopology: true | |
}); | |
routes.init(server); | |
}; | |
let start = () => { | |
let hostname = server.get("hostname"), | |
port = server.get("port"); | |
server.listen(port, function() { | |
console.log( | |
"Express server listening on - http://" + hostname + ":" + port | |
); | |
}); | |
}; | |
module.exports = { | |
create: create, | |
start: start | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment