DEV Community

Vishnu
Vishnu

Posted on

Signup HTML file open in NodeJS server

Hello Developers,
In this Post, i will cover this following things :

  • create a server in Nodejs
  • how to use experss module
  • how to open html file in nodejs server

Step 1. Install NodeJS

Step 2. run npm init

Step 3. now we need to create a server

we have mainly two way

  1. create server with http module (inside)
  2. create server with express module (outside)

i am goning to use express module. so we need to

Step 4. run this command npm install express

Step 5. npm install open

i am goning to use open module is using open web browser after server has connect

Step 6. create a file index.js and write this code

index.js

Step 7. now create a html file index.html

index.html

now question is how to open html file on nodejs server??
we have many way to do this...
we can use fs, http-server, express modules..
i am going to use express module. it's easy and popular.

Step 8. replace this code

res.sendFile(`${__dirname}\\index.html`);
Enter fullscreen mode Exit fullscreen mode

remove this code res.send('Welcome')

one more thing

Step 9. add this script in package.json "start": "node index.js"

Step 10. run this command npm start

Output
if you want to download this code. i am sharing this code on github link [https://github.com/whovishnu/express-server-nodejs]

ThankYou

Top comments (0)