-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverChat.js
43 lines (34 loc) · 1.11 KB
/
serverChat.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
var express = require('express');
var LISTEN_PORT = 8296;
/* Create an express application. `app` is a common variable name used in many express.js tutorials */
var app = express();
/**
* The bodyParser automatically handles the parsing of JSON requests for us. Don't forget to include it
* if you expect to process JSON data in your HTTP requests.
*/
app.use(express.bodyParser());
// listen to the client
app.use(express.static(__dirname));
// all the messages
var allMsgs = [];
/*
* app.post() defines an handler for HTTP GET requests. You can peek into the request data inside req.body.
*/
app.post('/chat', function (req,res) {
if (req.body.message) {
console.log('message received: "' + req.body.message + '"');
allMsgs.push(req.body.message);
console.log('allMsgs = ' + allMsgs.join("\n"));
}
res.send(null);
});
app.get('/chat', function (req, res) {
// return all the chat msgs
res.json(allMsgs);
});
/*
* app.listen() starts the HTTP server on the given TCP port.
*/
app.listen(LISTEN_PORT, function () {
console.log('Listening on ' + LISTEN_PORT + '...');
});