-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (25 loc) · 814 Bytes
/
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
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/messages', function (req, res) {
res.status(200).send(['Hello there', 'this is another message, yay.']);
});
app.post('/message', function (req, res) {
if (!req.body || !req.body.msg) return res.sendStatus(400);
io.emit('chat message', req.body.msg);
res.sendStatus(200);
});
app.post('/messages', function (req, res) {
if (!req.body || !Array.isArray(req.body)) return res.sendStatus(400);
req.body.forEach(function (item) {
io.emit('chat message', item.msg);
});
res.sendStatus(200);
});
http.listen(3000, function () {
console.log('Let\'s go');
});