node.js - How to get a HTML page in nodejs? -


test.html

<html>     <head>         <title>test page</title>     </head>     <body> body</body> </html> 

how 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 @ http://127.0.0.1:1337/'); 

to return test.html above?

here's example of 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 should use express.static: http://runnable.com/uww3g0pkxoawaa6k

or deticated static module https://github.com/jesusabdullah/node-ecstatic


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -