-1

I wanted to serve the Disp.html file through nodejs following the code I learned through a Youtube tutorial.

const http = require("http");
const fs = require("fs");
const fileContent = fs.readFileSync("Disp.html");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-type": "text/html" });
  res.end(fileContent);
});

server.listen(80, "127.0.0.1", () => {
  console.log("Listening on port 80");
});

I have created a folder in VS Code with this app.js and Disp.html as the files. Every time I run the code it displays the listing directory in place of the HTML content that I am expecting to view.

What can I do to get the expected result?

Raghul SK
  • 1,256
  • 5
  • 22
  • 30
Misha
  • 9
  • 1
  • Something is very wrong with how you are running it. It *sounds* like you might be running the VS Code live server and accessing that instead of running the server you're written in the above code. – Quentin Jul 15 '20 at 15:16
  • Seems like you got this file outside the www directory, start the node app and have the correct files in the public directory, or what ever name it's for you. – PatricNox Jul 15 '20 at 15:17
  • Note that dealing with `http.createServer` directly is almost always far more annoying than your time is worth and that you're usually better off using Express.js. – Quentin Jul 15 '20 at 15:17
  • @PatricNox — No. There's no `www` directory because this isn't Apache/IIS/whatever. If the `Disp.html` was in the wrong place then it would error trying to read it. – Quentin Jul 15 '20 at 15:18

1 Answers1

-1

Thanks, I got it. The local host was at port 5500 not 80, changing that in the code got me the html file served.

So it was server.listen(5500, ...)

Misha
  • 9
  • 1