How to Create First Hello World Application in Node Js?

  9 months ago     Views:  814  
Cole Bartell
1 Follows


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 :

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.

NodeJs - How to install latest version in Ubuntu 16.04

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 download this project from Github link Download

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. Also please follow us on Twitter and Like our Facebook page for updates.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

1 Comments

  • Image
    Lemuel
    December 31, 2019 Reply

    Howdy would you mind stating which blog platform you're working with?

    I'm planning to start my own blog in the near future but
    I'm having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style
    seems different then most blogs and I'm looking for something completely unique.

    P.S Apologies for being off-topic but I had to ask! It is appropriate time to make some plans for the future and it is time
    to be happy. I’ve read this post and if I could I wish to
    suggest you few interesting things or advice. Maybe you could write next articles referring to
    this article. I wish to read more things about it!
    Ahaa, its pleasant dialogue regarding this post here at this webpage, I have read all that,
    so now me also commenting here. http://porsche.com