|
var http = require('http'); |
|
var port = process.env.port || 3000; |
|
var server = http.createServer(); |
|
|
|
var log = function(req) { |
|
console.log(req.method + ' ' + req.url); |
|
}; |
|
|
|
var router = { |
|
'/progressive': function(req, res) { |
|
res.statusCode = 200; |
|
res.setHeader("Content-Type", "text/html"); |
|
res.write([ |
|
'<html>' |
|
, ' <head>' |
|
, ' <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>' |
|
, ' <style type="text/css">' |
|
, ' html, body {' |
|
, ' background: red;' |
|
, ' font-family: sans-serif;' |
|
, ' }' |
|
, ' </style>' |
|
, ' </head>' |
|
, ' <body>' |
|
, ' <h1>Hello world</h1>' |
|
].join('\n')); |
|
setTimeout(res.write.bind(res, '<h1>blah</h1>'), 5000); |
|
setTimeout(res.write.bind(res, [ |
|
'<style type="text/css">' |
|
, ' html, body {' |
|
, ' background: blue;' |
|
, ' }' |
|
, '</style>' |
|
].join('\n')), 8000); |
|
setTimeout(res.write.bind(res, '<h1>blah</h2>'), 10000); |
|
setTimeout(res.write.bind(res, '</body></html>'), 15000); |
|
setTimeout(res.end.bind(res), 20000); |
|
}, |
|
|
|
'/blocking': function(req, res) { |
|
res.statusCode = 200; |
|
res.setHeader('Content-Type', 'text/html'); |
|
setTimeout(function() { |
|
res.write('one<br>two<br>three<br>four<br>'); |
|
res.end(); |
|
}, 4000); |
|
} |
|
}; |
|
|
|
var route = function(req, res) { |
|
log(req); |
|
if (req.url in router) { |
|
router[req.url].call(router, req, res); |
|
} else { |
|
res.write('Could not find route ' + req.url); |
|
res.statusCode = 404; |
|
res.end(); |
|
} |
|
}; |
|
|
|
server.on('request', route); |
|
|
|
server.listen(port, function() { |
|
console.log('listening on port', port); |
|
}); |