-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
91 lines (70 loc) · 2.02 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
85
86
87
88
89
90
91
const express = require('express');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const pg = require('pg');
/**
* ===================================
* Configurations and set up
* ===================================
*/
const configs = {
user: 'akira',
host: '127.0.0.1',
database: 'testdb',
port: 5432,
};
const pool = new pg.Pool(configs);
pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack);
});
// Init express app
const app = express();
// Set up middleware
app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(express.urlencoded({
extended: true
}));
// 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
* ===================================
*/
// Root GET request (it doesn't belong in any controller file)
app.get('/', (request, response) => {
response.send('Welcome To Tweedr.');
});
app.get('/users/new', (request, response) => {
response.render('user/newuser');
});
app.post('/users', (request, response) => {
const queryString = 'INSERT INTO users (name, password) VALUES ($1, $2)';
const values = [
request.body.name,
request.body.password
];
// execute query
pool.query(queryString, values, (error, queryResult) => {
//response.redirect('/');
response.send('user created');
});
});
/**
* ===================================
* Listen to requests on port 3000
* ===================================
*/
const server = app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));
let onClose = function(){
server.close(() => {
console.log('Process terminated')
pool.end( () => console.log('Shut down db connection pool'));
})
};
process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);