-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.js
72 lines (59 loc) · 1.46 KB
/
web.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
64
65
66
67
68
69
70
71
72
var express = require('express');
var chat = require('./lib/chat.js');
var app = express.createServer(express.logger());
app.configure(function () {
app.use(express.logger());
app.use(express.static(__dirname + '/static'));
app.use(express.bodyParser());
app.use(app.router);
app.use(express.methodOverride());
});
app.configure('development', function () {
app.use(express.errorHandler({
dumpExceptions: true,
showStack:true
}));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
app.set('views', __dirname + '/views');
app.set('view options', { layout: false });
app.register('.html', {
compile: function(str, options){
return function(locals){
return str;
};
}
});
app.get('/', function(req, res) {
res.render('index.html');
});
function error(err, data) {
console.log("ERROR: " + err);
console.log("DATA: " + data);
}
app.get('/ws/chat', function(req, res) {
chat.get(function (chats) {
res.send(chats);
}, error);
});
app.post('/ws/chat', function(req, res) {
chat.add(req.body, function (chat) {
res.send(chat);
}, error);
});
app.put('/ws/chat/:id', function(req, res) {
chat.save(req.params.id, req.body, function (chat) {
res.send(chat);
}, error);
});
app.del('/ws/chat/:id', function(req, res) {
chat.remove(req.params.id, function () {
res.send();
}, error);
});
var port = 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});