UNPKG

797 BJavaScriptView Raw
1var http = require('http'),
2 director = require('../lib/director');
3
4var router = new director.http.Router();
5
6var server = http.createServer(function (req, res) {
7 req.chunks = [];
8 req.on('data', function (chunk) {
9 req.chunks.push(chunk.toString());
10 });
11
12 router.dispatch(req, res, function (err) {
13 if (err) {
14 res.writeHead(404);
15 res.end();
16 }
17
18 console.log('Served ' + req.url);
19 });
20});
21
22router.get(/foo/, function () {
23 this.res.writeHead(200, { 'Content-Type': 'text/plain' });
24 this.res.end('hello world\n');
25});
26
27router.post(/foo/, function () {
28 this.res.writeHead(200, { 'Content-Type': 'application/json' });
29 this.res.end(JSON.stringify(this.req.body));
30});
31
32server.listen(8080);
33console.log('vanilla http server with director running on 8080');