-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
58 lines (49 loc) · 1.61 KB
/
app.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
var express = require('express');
var app = module.exports = express();
var config = require('./config.js')(app, express);
var jade = require('jade');
var fs = require('fs');
var kibitzDM = function(){
var key = 'kibitzs.jade';
var path = __dirname + '/views/'+key;
var str = jade.cache[key] || (jade.cache[key] = fs.readFileSync(path, 'utf8'));
var kibView = jade.compile(str);
var kibItems = [];
var add = function(user,msg){
kibItems.push({date: 1, from: user, text: msg});
var l = kibItems.length;
if (l > 5)
kibItems = kibItems.slice(l-5,l);
};
add( 'guest', 'first post');
var getView = function(user){
return kibView({
username: user,
kibItems: kibItems
});
};
return {
getView : getView,
kibItems : kibItems,
add : add
};
}();
app.get('/', function(req, res){
req.session.username = req.session.username||'guest';
console.log( 'render : ' + kibitzDM.kibItems);
res.render('index.jade', {
username: req.session.username,
kibItems: kibitzDM.kibItems
});
});
app.post('/posta', function(req, res){
req.session.username = req.body.from;
if (req.body.newtext)
kibitzDM.add(req.session.username, req.body.newtext);
io.sockets.emit('kibView', kibitzDM.getView(req.session.username));
res.send('');
});
console.log(process.env.PORT);
var io = require('socket.io').listen(app.listen(process.env.PORT));
// fall back to ajax long-polling, AppFog doesn't support websockets.
io.set('transports', ['xhr-polling']);