Node itself can serve static files without express or any other module..?

前端 未结 5 2299
眼角桃花
眼角桃花 2021-02-20 06:28

I am beginner in the field of node js.No idea how to send simple request from url Like :- http://localhost:9999/xyz/inde.html my file hierarchy is

server.js
xyz         


        
5条回答
  •  余生分开走
    2021-02-20 06:50

    its very simple, node already provide fs module from which u can read that html file and append on response obj like this:

    response.writeHead(200, {"Content-Type": "text/plain"});
    //here is the code required
    fs.readFile("./xyz/index.html", (err,fileContent) =>
    {
      response.end(fileContent);
    });
    

    but the problem here is you will only get the HTML document not the resources inside that HTML files which are stored in different folders like if you have this code in your index.html

    this index.css will not be allowed by the node server. But i guess your question is solved.

提交回复
热议问题