Method 1: Use Express to host static files, details see here
Method 2: Use the ReadFile method provided by the FS module to open the file so that it is output in text/html form.
Code:
var express = require (' Express ');var fs=require ("FS");var app =Express ();//Method 1: Access the static file via Express.static, which is accessed by ajax.html//App.use (Express.static ("./"));//Method 2: Use Fs.readfile to open the HTML file app.get ("/helloworld.html",function (request, response) {Fs.readfile ("./" +request.path.substr (1), function (err,data) {// body if (Err) {Console.log (err); //404:not FOUND response.writehead (404,{"Content-type": "Text/html" else{//200:ok response.writeHead ( 200,{"Content-type": "text/html" }); Response.Write (Data.tostring ()); } response.end (); });}); App.listen (function () {// Monitor HTTP// 127.0.0.1:3000 Port Console.log ("Server Start"
browser access, enter http://127.0.0.1:3000/hello_static.html and Http://127.0.0.1:3000/respectively hello_fs.html, Result:
 
Two ways to open HTML files in the node server