-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (32 loc) · 841 Bytes
/
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
var port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}))
//messages object array
let messages = [{
name: 'Tom',
message: 'Hi, bilal how are you?'
},
{
name: 'John',
message: 'Hi, buddy what\'s going on'
}
];
// define a route to serve those messages to client
app.get('/messages', (req, res) => {
res.send(messages);
})
// to receive messages from clients
app.post('/messages', (req, res) => {
messages.push(req.body);
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});