-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (56 loc) · 1.51 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require('dotenv').config();
const http = require('http');
const bodyParser = require('body-parser');
const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const hbs = require('hbs');
const views = require('./routes/views');
const api = require('./routes/api');
// initialize express app
const app = express();
app.set('view engine', 'hbs');
hbs.registerPartials(__dirname + '/views/partials');
// set POST request body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// connect to db
const url = process.env.MONGO_SRV;
mongoose.set('useFindAndModify', false);
mongoose.connect(url, {useNewUrlParser: true}, function (err) {
if (err) {
console.log("MongoDB connection error:");
console.log(err);
} else {
console.log("MongoDB connected");
}
});
// set up sessions
app.use(session({
secret: 'session-secret',
resave: 'false',
saveUninitialized: 'true'
}));
app.use('/', views);
app.use('/api', api);
app.use('/static', express.static('public'));
// 404 route
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// route error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send({
status: err.status,
message: err.message,
});
});
// port config
const port = 3000; // config variable
const server = http.Server(app);
server.listen(port, function() {
console.log('Server running on port: ' + port);
});