-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 1.75 KB
/
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
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
"use strict";
var express = require('express');
var app = express();
var bodyParser = require('body-parser')
// Serve static files (css, js, images) out of the public directory
app.use(express.static('public'));
// When we get a request with an empty path,
// send our `index.html` file.
//
app.get('/', function (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
// Keep all our chats in an array and populate it
// with some starter messages.
//
var chats = ['first message', 'second message'];
// When we receive an HTTP GET request like `/chats/2`,
// send an array of all the chats after index `2` in
// JSON format.
//
app.get('/chats/:latest', function(req, res){
try {
var latest = parseInt(req.params.latest);
} catch (e) {
res.status(400);
res.send('Bad request');
return;
}
var latestChats = chats.slice(latest);
console.log('Received requests for chats since #', latest, 'of which there are', latestChats.length);
res.json(latestChats);
});
// When we receive a POST request to `/chats`, parse
// any submitted forms and push the new chats onto
// our `chats` array. Also accept JSON.
//
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.post('/chats', function(req, res){
var newMessage = req.body.message;
if(newMessage && newMessage.length > 0){
console.log('Received new chat message: ', message);
chats.push(newMessage);
}
res.end();
})
// Start-up the application
//
var port = process.env.PORT || 3000;
app.listen(port, function () {
var url = 'http://localhost:' + port;
if (process.env.C9_HOSTNAME) {
url = 'https://' + process.env.C9_HOSTNAME;
}
console.log('Chat app running at ', url);
});