-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (39 loc) · 1.8 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const config = require('./config'); // Helps to obscure login info (dotenv)
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const expressLayouts = require('express-ejs-layouts');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const mongoose = require('mongoose');
const session = require('express-session');
const app = express();
// Set sessions and cookie parser
app.use(cookieParser());
app.use(session({
secret: process.env.SECRET, // Additional security for session (needs real authentication)
cookie: { maxAge: 60000 }, // Keep the cookie
resave: false, // Forces the session to be saved
saveUninitialized: false // Dont save unmodified
}));
// This allows success and error messages to be available to the next page
// that the user is redirected to (order created successfully from last
// order on the /create route)
app.use(flash());
// Static assets (only css here)
app.use(express.static(__dirname + '/public'));
// Set ejs as templating engine
app.set('view engine', 'ejs');
app.use(expressLayouts);
// Env is required to log into mLab and must be provided separately
mongoose.connection.openUri(`mongodb://${config.db.username}:${config.db.password}@${config.db.host}/${config.db.dbName}`, { useNewUrlParser: true });
// Use body parser used for parsing urls
app.use(bodyParser.urlencoded({ extended: true }));
// Validate input from user (make sure that forms are not empty)
app.use(expressValidator());
// Set routes
app.use(require('./app/routes'));
// Start server
app.listen(config.port, () => {
console.log(`${config.appName} is up and running on port: ${config.port}`);
});