forked from shakacode/react-webpack-rails-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (43 loc) · 1.34 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
/* eslint-disable no-console, func-names, no-var */
var bodyParser = require('body-parser');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.hot.config');
var sleep = require('sleep');
var comments = [
{author: 'Pete Hunt', text: 'Hey there!'},
{author: 'Justin Gordon', text: 'Aloha from @railsonmaui'},
];
var server = new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
noInfo: false,
stats: {
colors: true,
hash: false,
version: false,
chunks: false,
children: false,
},
});
server.app.use(bodyParser.json(null));
server.app.use(bodyParser.urlencoded({extended: true}));
server.app.get('/comments.json', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(comments));
});
server.app.post('/comments.json', function(req, res) {
console.log('Processing comment: %j', req.body.comment);
console.log('(shhhh...napping 1 seconds)');
sleep.sleep(1);
console.log('Just got done with nap!');
comments.push(req.body.comment);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(req.body.comment));
});
server.listen(3000, 'localhost', function(err) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3000...');
});