bluehost-banner
Hello world in Node.js with Express Framework (Updated)

Hello world in Node.js with Express Framework (Updated)

Howdy, coders, Hope you doing well!🙂

In the previous getting-started-with-node.js post we covered some basic introduction about node.js,

In this tutorial, we will take one step ahead and will create first hello world program in node.js with using the express.js framework.

This article is aimed for beginner developers and anyone interested in getting up and running with Node.js

If you don’t have knowledge of node.js then please check getting-started-with-node.js article first else continue here.

so, let’s get started…

How to Create First App in Node.js

Step-1: Install Node.js for your specific platform(Windows, Linux or macros)

Just go to nodejs.org in your browser address bar and you should be good to go, the home screen will automatically detect what you want, as I am running windows on my machine it will show respective version of node.js available for Windows operating system. go ahead and download and install in your machine, This will give you the tools needed to run a server on your local machine.

Download-NodeJS
Download-NodeJS

Step-2: Open command prompt and type:

mkdir helloworldapp
cd helloworldapp

Above commands will create a blank directory with name â€śhelloworldapp“ and then move to that directory.

Step-3: Initialize your app and  link with NPM:

Type following command to initialize your app:

npm init

This command will ask you to enter a number of things, you can enter it your way except this one:

entry point: (index.js)

You need to change this to:

entry point: (index.js)  app.js

This will create package.json file in your “helloworldapp” project folder. this file will contain all packages you have downloaded for your projects with specific version details.

To learn more about NPM please click here.

Step-4: Install Express in project directory

now you’re thinking what is express right?

What is express?

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

It is an open source framework developed and maintained by the Node.js foundation

So, to install express go to  command prompt and type following command:

npm install express --save

This will install the latest version of the express framework for your project, now you will be able to see node_modules folder in your project root directory, it contains all dependencies required for the express framework and â€“save command  will save the package to your dependencies list, located in the package.json file

Step-5: Open your text editor and create an app.js file in root directory and write below code:

var express = require("express");
var app = express();
app.get("/", function(req, res) {
  res.send("Hello World!");
});
app.listen(3000, function() {
  console.log("listening on port 3000!");
});

Here we have included express package which we have recently installed with using required method in the first line, by assigning module to variable here we can make use of its predefined set of tools.

The listen method starts a server and listens on port 3000 for connections.It responds with “Hello World!” to get requests to the root URL (/). For every other path, it will respond with a 404 Not Found

Step-6: Run the app

Type below command:

node app.js

this command will start listing your server on 3000 port, to view open your browser and go to http://localhost:3000 it will show result printed “Hello world” in browser and also you will be able to see “listening on port 3000!”  message in your terminal.

👍That’s it, you’re done. You have successfully created your first Node app.

Conclusion:

Thanks for reading.

Do let me know If you face any difficulties please feel free to comment below we love to help you. if you have any feedback suggestions then please inform us by commenting.

Don’t forget to share this tutorial with your friends on Facebook and twitter.

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment