Node Js one of the fastest-growing programming language, and you can develop web application desktop applications and also make mobile application help of node js. in this article we will share with you how to create your first hello world web application using node js.
You are you never work with node js before then don't worry this article for you. here we are starting to very basic how to create your first web application in node js steps by steps
All Steps:
- Step 1 : Install the Node Js
- Step 2 : Create package.json File
- Step 3 : Install Package
- Step 4 : Create server.js File
- Step 5 : Create index.ejs File
- Step 6 : Run Application in Browser
Application Structure
Here, you can see how your first node js application structure looks like :
helloworldapp
├── node_modules
├── views
│ ├── index.ejs
├── package.json
└── server.js
Install the Node Js
In the first step, we need to install nodejs
in our local system. please follow the article and first install the nodejs
if you have not installed in your system.
Step - 2 : Create package.json File
Now, go to your application folder's root path and first create the package.json
file by running this command.
npm init
After, hit this command in your terminal then put all the required data for and first done it. then your package.json
the file looks like.
{
"name": "firstnodejsapp",
"version": "1.0.0",
"description": "how to create hello world web application in nodejs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [
],
"author": "Harsukh Makwana",
"license": "ISC",
}
Step - 3 : Install Package
After, done the create package.json
the file then we need to install some required package for our node js application.
First, we need to install express
and ejs
package in our node js application running by the following command in our terminal.
sudo npm i express ejs
You will use ejs
file system in our node js application so, ejs
package required for our views files.
Then we need to install another package name nodemon
package for run the node server in our local system. run the following command in your terminal.
npm i --save-dev nodemon
After, install all the required packages in your application then open the package.json
file and change the following. then our package.json
file will look like.
{
"name": "firstnodejsapp",
"version": "1.0.0",
"description": "how to create hello world web application in nodejs",
"main": "index.js",
"scripts": {
"devStart": "nodemon server.js"
},
"keywords": [
],
"author": "Harsukh Makwana",
"license": "ISC",
"dependencies": {
"ejs": "^3.0.1",
"express": "^4.17.1"
},
"devDependencies": {
"dotenv": "^8.2.0",
}
}
we should make a change in scripts
for run our node js application.
Step - 4 : Create server.js File
Now, create one server.js
file in your application root path and copy the above code and paste it into.
const express = require('express')
const app = express()
app.set('view-engine', 'ejs')
app.get('/', (req, res) => {
res.render('index.ejs')
});
app.listen(3000)
Step - 5 : Create index.ejs File
Now in the last step, we will create views/index.ejs
file and write the following simple HTML code into it.
<!DOCTYPE html>
<html>
<head>
<title>First Hello World Application In Node Js - HackTheStuff</title>
</head>
<body>
<h1>Hello World!!</h1>
</body>
</html>
Step - 6 : Run Application in Browser
Now, into the last how to run this node js application in a web browser. so, simply run the following command into the terminal on your node js application root path.
sudo npm run devStart
After the hit command in your terminal then the output will be shown into the terminal looks like.
> helloworldapp@1.0.0 devStart /var/www/nodejs/helloworldapp
> nodemon server.js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Now, your first node js application ready to run open your web browser and hit the following URL into it.
http://localhost:3000
Conclusion
You can see create the first node js web application is very easy to build. You can check I hope you will like this article.