In this article, we are going to learn about one of the basic and most used packages with Node.js which is Express. We assume that till now you are having Node.js pre installed in your systems, if not then head over to this article.

Let’s Start 

Now, first of all, create a folder and name it as “firstApplication” or whatever you want to, it entirely depends on you.

Now head over to the terminal and navigate to the directory in which you have just created this folder and then type the following command:

$   cd firstApplication

NPM

Now the real fun begins! As you have created your directory, you will need to initialize a project and link it to npm. Now you will be guessing what npm is. It is the place where all the Node packages live.

Go and type this command in your terminal which initializes your project.

$   npm install

This creates a package.json file in your “firstApplication” folder. The file contains references for all npm packages you have downloaded to your project.

Installing express

Express.js is a web application framework for Node.js. It provides various features that make web application development fast and easy which otherwise takes more time using only Node.js. 

Now to install express, run the following command in your terminal.

$   npm install express –save

You will now be able to see a node_modules folder get created in the root of your project.

Lets Code


var express = require( ‘express’ );
var app = express();

app.get(‘/’,function(req,res){  res.send(“Hello World!”);
});

app.listen(3000,function(){
console.log(“App Listening on port 3000!”);
});

  • The first line declares a variable which will contain the module called express, grabbing it from the node_modules folder. 
  • The module is actually a function. Assigning the function call to another variable gives you access to a predefined set of tools which will in a great deal make your life much easier. 
  • You could view the variable app as an object, whose methods you are utilizing to build the actual program.
  • The listen method starts a server and listens on port 3000 for connections.
  • It responds with “Hello World!” for get requests to the root URL (/). For every other path, it will respond with a 404 Not Found.

Ready to Rock!

Now head back to your terminal and type the following command:

$   node app.js

After running the command, load http://localhost:3000/ in a browser to see the output.

  • You must also be seeing “App Listening on port 3000!” logged into your terminal.
  • Now before closing the terminal press Ctrl+c to free the terminal.
  • To start your application again, use the same command “node app.js”.
  • That’s it. You are all done.


That’s all I have and thanks a lot for reading. Please let me know if any corrections/suggestions. Please do share and comments if you like the post. Thanks in advance… 😉

Thanks Yash Kesharwani for helping us to grow day by day. He is expert in the backend services and loves to play around node.js.

Categories: Nodejs

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert