Home > Software engineering >  Is there a way for me to supply the http server in NodeJS with an html file for hosting?
Is there a way for me to supply the http server in NodeJS with an html file for hosting?

Time:02-04

Current code below

const http = require('http');
const server = http.createServer(function(req, res) {
  res.setHeader("Content-type", "application/json");
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.writeHead(200);
  res.end("Ping server page");
});

server.listen(8080, function() {
  console.eblue("\nListening on port 8080");
  console.eblue("WEB SERVER STARTED\n");
});

I just want to know if their is a way to make a fully featured site and host it using node.js

CodePudding user response:

I recommend using Express (a node.js package) for web servers, which you can then serve static files - including HTML - through with express.static.

CodePudding user response:

Yes (if I understood correctly), you just need to serve the html as a static file like this:

https://expressjs.com/en/starter/static-files.html

  •  Tags:  
  • Related