-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (35 loc) · 1.26 KB
/
index.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
const express = require('express');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const db = require('./db');
const PORT = process.env.PORT || 5000;
// Init express app
const app = express();
// Set up middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(methodOverride('_method'));
app.use(cookieParser());
// Set handlebars to be the default view engine
app.engine('handlebars', handlebars.create().engine);
app.set('view engine', 'handlebars');
// Import routes to match incoming requests
require('./routes')(app, db);
// Root GET request (it doesn't belong in any controller file)
app.get('/', (request, response) => {
response.render('home');
});
// Catch all unmatched requests and return 404 not found page
app.get('*', (request, response) => {
response.render('404');
});
const server = app.listen(PORT, () => console.log('~~~ Tuning in to the waves of port '+PORT+' ~~~'));
// Run clean up actions when server shuts down
server.on('close', () => {
console.log('Closed express server');
db.pool.end(() => {
console.log('Shut down db connection pool');
});
});