Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run index.html on Node.JS

I need to run a website on a server. I installed Node.JS and created a server.js file in the project folder and read the index.html file there. The site opens at the address, but the error in the console is Uncaught SyntaxError: Unexpected token ‘<‘. And the site does not include styles. How can this problem be solved?

// server.js

var http = require("http");
var fs = require("fs");

const PORT = 8080;

fs.readFile(
  "../index.html",
  function (err, html) {
    if (err) throw err;

    http
      .createServer(function (request, response) {
        response.writeHeader(200, { "Content-Type": "text/html" });
        response.write(html);
        response.end();
      })
      .listen(PORT);
  }
);

Error:enter image description here

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are sending the html file in a .js file to the browser. The Problem with the style is caused because you don’t send the css file with. You could use ExpressJs to send the whole directory:

https://www.digitalocean.com/community/tutorials/nodejs-serving-static-files-in-express

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading