NodeJs

Developing an ExpressJs Application

Create Server Using Express Framework Noedjs Tutorial

In the previous lesson, we have learned about NodeJs, npm, and also given the introduction about Express framework. Now in this knowledge bit, we are going one more step ahead and will create our very first ExpressJs Application.

Create an ExpressJs Application:

After installing and setting up nodejs runtime and express framework (you can review our previous article if you don’t know how we can set up and install nodejs and express), we are going ahead and creating our very first Express application. So the first thing we need to do is create a new directory, and to create folder write the following command on your terminal.

[user] mkdir FirstExpressApp       

Then navigate to application folder directory using cd followed by folder name.

Build the First Express Application

 

Now we are going to create a new JavaScript file and then in that JavaScript file, we will write our Express code. This is how we will create the new file using the command line:

 

create the folder using command line

 

Now open that file in your text editor and test file by writing simple code. In our case, we are using the Sublime text editor. You can use any text that you are comfortable with. Now go ahead and run that JavaScript file in command line.

 

Navigate through the folder

 

After testing our nodeJs, we will install express in our application directory folder. To install express in your directory folder run the following command on the command line:

 

[user] npm install express       

 

It will take few seconds and then it will add the express module into our project:

 

how to install the Express framework into your directory folder

 

Now the next thing we need to do is include express in the ourApp.js file by requiring it. Then we will execute it and save it in the variable called app. Here is the demonstration below:

 

var express = require('express');       

 

This will include all the code and content of express framework in our application.

 

var app = express();       

 

Then we will use this code to execute and save it to any variable. In our case we, we are saving in the app variable.

We have express installed and initialized in our application, now let’s check whether it is working or not. Run the ourApp.js file, and if we do not get any error that’s mean it’s working fine.

 

Run Node Js Express server application using Command line

 

Now we are going to create the first route. Before creating the route, we will create the server and connect our application to the browser. We will do it with the help of using express method listen. We will write the following code in the ourApp.js file:

 

app.listen(3000, function() {

console.log('Hey there from the server');

});       

 

Now save ourApp.js file and run on the Command line. Then open your browser and navigate to localhost:3000, you will see the following output.

 

How to run the express server

 

And on our command line we have got the following output:

 

ExpressJs Application - run your first express nodejs server application

 

Now we will create two different routes page1, and page2 to get different responses for these three different responses. Whenever we visit localhost:3000 or some other URL it will print some text.

Here we will use get request method, and we will handle GET request with the get method.

 

app.get(path, callback)       

 

This method takes two arguments first is PATH and the second argument is Callback function.

PATH, the first argument, it could be anything that comes after your domain name. In our case, we are using “/” which is also called the route PATH

CALLBACK function, the second argument, it is used to tell the server that we to do next if the PATH is matched, the function also takes two objects as an argument, the first one is the request and the second one is the response. The request object contains all the information about the request that was made. While response object contains all the information about what we are going to respond with. Now write the following code into your JavaScript:

 

app.get('/', function(req, res) {
res.send('Hello From the Express Server!');

});       

 

Now run the server by running your JavaScript file and open browse and navigate to localhost:3000. You will get the following output:

 

Express framework node js server application

 

 

Now for the different route, we need to change the PATH, we will add two more get method /page1 and /page2. Here is the code below:

 

//For page 1
app.get('/page1', function(req, res) {
res.send('Hello from the page 1!');
});       

 

Now save your JavaScript file and restart your nodeJs server. Then navigate to localhost:3000/page1 in your browser. You will get the following output:

 

ExpressJs Application- Create different responses for different pages

 

//For page 2
app.get('/page2', function(req, res) {
res.send('Hello from the page 2!');
});       

 

For page2 repeat the same process that we have did for page1

 

How to Create different responses for different pages Express framework

Conclusion:

In this expressJs application, we have seen that how we can create a server using NodeJs. We have also discussed get response method and seen how we can create the page for different responses.

Click to comment

Leave a Reply

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

Most Popular

To Top