Skip to content

Instantly share code, notes, and snippets.

@artemis15
Created October 7, 2019 18:09
Show Gist options
  • Save artemis15/c2060f82f2c664f9f3897c39efca6700 to your computer and use it in GitHub Desktop.
Save artemis15/c2060f82f2c664f9f3897c39efca6700 to your computer and use it in GitHub Desktop.
App.js with JWT
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