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
- create server with http module (inside)
- 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
Step 7. now create a html file 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`);
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
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)