forked from initialstate/node-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
45 lines (34 loc) · 1.13 KB
/
start.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
var buckets = require('./buckets');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
// read the body of any content type as utf-8 text
app.use(bodyParser.text({ type: '*/*' }));
// Relay event data, securely, to Initial State
app.post('/:bucket/:event', function (req, res) {
if (typeof req.body === 'undefined') {
// there's no value
return res.sendStatus(400);
}
// If the request includes a "Date" header, use it
var date = new Date(req.get('date'));
if (isNaN(+date)) {
// ...but only if it's a valid date
date = void 0;
}
// send an event to Initial State
buckets.get(req.params.bucket).push(req.params.event, req.body || '', date);
// send back a successful response (HTTP 204 = It worked, that is all.)
res.status(204).end();
console.log(req.params.event, req.body);
});
// Show README.md as a homepage
app.engine('md', require('./md-view'));
app.set('views', __dirname);
app.set('view engine', 'md');
app.get('/', function (req, res, next) {
res.render('README');
});
app.listen(8080, function () {
console.log('Node-hub serving on port', this.address().port);
});