1 | var http = require('http'),
|
2 | director = require('../lib/director');
|
3 |
|
4 | var router = new director.http.Router();
|
5 |
|
6 | var 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 |
|
22 | router.get(/foo/, function () {
|
23 | this.res.writeHead(200, { 'Content-Type': 'text/plain' });
|
24 | this.res.end('hello world\n');
|
25 | });
|
26 |
|
27 | router.post(/foo/, function () {
|
28 | this.res.writeHead(200, { 'Content-Type': 'application/json' });
|
29 | this.res.end(JSON.stringify(this.req.body));
|
30 | });
|
31 |
|
32 | server.listen(8080);
|
33 | console.log('vanilla http server with director running on 8080');
|