-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (67 loc) · 1.97 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
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
const path = require('path');
const express = require('express');
const db = require('./db/postgres');
const parser = require('body-parser');
const app = express();
const jsonParser = parser.json({
verify(req, res, buf, enc) {
try {
JSON.parse(buf);
} catch (error) {
res.status(400).json({ Error: error.message });
throw error;
}
}
});
app.get('/api/comments', function (req, res) {
const query = 'SELECT id, username, email, content, date'
+ ' FROM comments'
+ ' ORDER BY date';
db.query(query, [], (dbError, dbRes) => {
if (dbError)
res.status(500).json('Database error: ' + dbError);
else
res.json(JSON.stringify(dbRes.rows));
});
});
app.post('/api/comments', jsonParser, function (req, res) {
const body = req.body;
const query = 'INSERT INTO comments'
+ ' (username, email, content)'
+ ' VALUES ($1, $2, $3)';
db.query(query, [ body.username, body.email, body.content ], (dbError, dbRes) => {
if (dbError)
res.status(500).json('Database error: ' + dbError);
else {
res.json(JSON.stringify(dbRes.rowCount));
}
});
});
app.delete('/api/comments/:id', function (req, res) {
const query = 'DELETE FROM comments WHERE id = $1';
db.query(query, [ req.params.id ], (dbError, dbRes) => {
if (dbError)
res.status(500).json('Database error: ' + dbError);
else
res.json(JSON.stringify(dbRes.rowCount));
});
});
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack');
const config = require('./webpack.config.dev');
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
}
app.use(express.static('public'));
const port = process.env.APP_PORT || 8000;
app.listen(port, function (err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at port %d', port);
});