Reputation: 11317
test.html
<html>
<head>
<title>Test Page</title>
</head>
<body> This is the body</body>
</html>
How do I modify this:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
to return test.html above?
Upvotes: 0
Views: 475
Reputation: 17319
Here's an example of a simple streaming static server
var basepath = '/files'
http.createServer(function (req, res) {
if (req.method !== 'GET') {
res.writeHead(400);
res.end();
return;
}
var s = fs.createReadStream(path.join(basepath, req.path));
s.on('error', function () {
res.writeHead(404);
res.end();
});
s.once('fd', function () {
res.writeHead(200);
});
s.pipe(res);
});
In practice you should use express.static: http://runnable.com/UWw3g0PKxoAWAA6K
Or a deticated static module like https://github.com/jesusabdullah/node-ecstatic
Upvotes: 1