Loading basic HTML in Node.js

后端 未结 20 1373
暗喜
暗喜 2020-11-28 17:34

I\'m trying to find out how to load and render a basic HTML file so I don\'t have to write code like:

response.write(\'...

blahblahblah

...\
20条回答
  •  星月不相逢
    2020-11-28 17:59

    This is a pretty old question...but if your use case here is to simply send a particular HTML page to the browser on an ad hoc basis, I would use something simple like this:

    var http = require('http')
    ,       fs = require('fs');
    
    var server = http.createServer(function(req, res){
      var stream = fs.createReadStream('test.html');
      stream.pipe(res);
    });
    server.listen(7000);
    

提交回复
热议问题