-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
51 lines (40 loc) · 1.12 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
require('coffee-script');
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.use(require('connect-assets')());
var http = require('http')
var server = http.createServer(app)
var io = require('socket.io').listen(server);
var websocket = require('./lib/websocket')(io);
var Ragot = require('./lib/models/ragot');
require('./lib/helpers/dateHelper');
var logger = function(req, res, next) {console.log(req.body); next();};
var auth = express.basicAuth(function(user, pass) {
return pass == process.env.PASSWORD;
});
app.use(logger);
if(process.env.PASSWORD) app.use(auth);
app.get('/', function(req, res) {
Ragot.findAll(function(err, ragots) {
res.render('index.jade', { ragots: ragots });
});
});
app.post('/ragots', function(req, res) {
if(!req.body.ragot.message) {
res.status(403);
res.end();
}
var ragot = new Ragot(req.body.ragot.message);
ragot.save(function(err) {
if(err) {
res.status(422);
} else {
websocket.broadcastRagot(ragot);
res.status(201);
}
res.end();
});
return null;
});
server.listen(process.env.PORT || 3000);