0

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?

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.