-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathindex.js
84 lines (61 loc) · 1.75 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
console.log("starting up!!");
const express = require('express');
const methodOverride = require('method-override');
const pg = require('pg');
// Initialise postgres client
const configs = {
user: 'YOURUSERNAME',
host: '127.0.0.1',
database: 'tunr_db',
port: 5432,
};
const pool = new pg.Pool(configs);
pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack);
});
/**
* ===================================
* Configurations and set up
* ===================================
*/
// Init express app
const app = express();
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(methodOverride('_method'));
// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();
app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');
app.engine('jsx', reactEngine);
/**
* ===================================
* Routes
* ===================================
*/
app.get('/', (request, response) => {
// query database for all pokemon
// respond with HTML page displaying all pokemon
response.render('home');
});
app.get('/new', (request, response) => {
// respond with HTML page with form to create new pokemon
response.render('new');
});
/**
* ===================================
* Listen to requests on port 3000
* ===================================
*/
const server = app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));
let onClose = function(){
console.log("closing");
server.close(() => {
console.log('Process terminated');
pool.end( () => console.log('Shut down db connection pool'));
})
};
process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);