DEV Community

Vishnu
Vishnu

Posted on

2 1

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay