-
Notifications
You must be signed in to change notification settings - Fork 30
/
simple.js
42 lines (34 loc) · 1 KB
/
simple.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
var fs = require('fs'),
path = require('path'),
union = require('../../lib'),
director = require('director'),
favicon = require('./middleware/favicon');
var router = new director.http.Router();
var server = union.createServer({
before: [
favicon(path.join(__dirname, 'favicon.png')),
function (req, res) {
var found = router.dispatch(req, res);
if (!found) {
res.emit('next');
}
}
]
});
router.get('/foo', function () {
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
this.res.end('hello world\n');
});
router.post('/foo', { stream: true }, function () {
var req = this.req,
res = this.res,
writeStream;
writeStream = fs.createWriteStream(__dirname + '/' + Date.now() + '-foo.txt');
req.pipe(writeStream);
writeStream.on('close', function () {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('wrote to a stream!');
});
});
server.listen(9090);
console.log('union with director running on 9090');