0

测试.html

<html>
    <head>
        <title>Test Page</title>
    </head>
    <body> This is the body</body>
</html>

我该如何修改:

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/');

返回上面的test.html

4

1 回答 1

1

这是一个简单的流式静态服务器的示例

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);
});

在实践中,您应该使用 express.static:http ://runnable.com/UWw3g0PKxoAWAA6K

或像https://github.com/jesusabdullah/node-ecstatic这样的专用静态模块

于 2013-04-18T01:25:36.317 回答