Express JS is an awesome opinionated framework for Node.js that helps you create REST end points. Let’s learn Express.js in 15 minutes!
In this tutorial, we will study the Express framework. This framework is built in such a way that it acts as a minimal and flexible Node.js web application framework, providing a robust set of features for building single and multipage, and hybrid web application.
In this tutorial, you will learn:
Express.js is a Node js web application server framework, which is specifically designed for building single-page, multi-page, and hybrid web applications.
It has become the standard server framework for node.js. Express is the backend part of something known as the MEAN stack.
The MEAN is a free and open-source JavaScript software stack for building dynamic web sites and web applications which has the following components;
1) MongoDB - The standard NoSQL database
2) Express.js - The default web applications framework
3) Angular.js - The JavaScript MVC framework used for web applications
4) Node.js - Framework used for scalable server-side and networking applications.
The Express.js framework makes it very easy to develop an application which can be used to handle multiple types of requests like the GET, PUT, and POST and DELETE requests.
Express gets installed via the Node Package Manager. This can be done by executing the following line in the command line
npm install express
The above command requests the Node package manager to download the required express modules and install them accordingly.
Let’s use our newly installed Express framework and create a simple “Hello World” application.
Our application is going to create a simple server module which will listen on port number 3000. In our example, if a request is made through the browser on this port number, then server application will send a ‘Hello’ World’ response to the client.
var express=require('express');
var app=express();
app.get('/',function(req,res)
{
res.send('Hello World!');
});
var server=app.listen(3000,function() {});
Code Explanation:
If the command is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Routing determine the way in which an application responds to a client request to a particular endpoint.
For example, a client can make a GET, POST, PUT or DELETE http request for various URL such as the ones shown below;
http://localhost:3000/Books
http://localhost:3000/Students
In the above example,
Each route can have one or more handler functions, which are executed when the route is matched.
The general syntax for a route is shown below
app.METHOD(PATH, HANDLER)
Wherein,
app is an instance of the express module
METHOD is an HTTP request method (GET, POST, PUT or DELETE)
PATH is a path on the server.
HANDLER is the function executed when the route is matched.
Let’s look at an example of how we can implement routes in the express. Our example will create 3 routes as
Our basic code will remain the same as previous examples. The below snippet is an add-on to showcase how routing is implemented.
var express = require('express');
var app = express();
app.route('/Node').get(function(req,res)
{
res.send("Tutorial on Node");
});
app.route('/Angular').get(function(req,res)
{
res.send("Tutorial on Angular");
});
app.get('/',function(req,res){
res.send('Welcome to Guru99 Tutorials');
}));
Code Explanation:
Here we are defining a route if the URL http://localhost:3000/Node is selected in the browser. To the route, we are attaching a callback function which will be called when we browse to the Node URL.
The function has 2 parameters.
If the command is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
From the output,
From the output,
From our above example, we have seen how we can decide on what output to show based on routing. This sort of routing is what is used in most modern-day web applications. The other part of a web server is about using templates in Node js.
When creating quick on-the-fly Node applications, an easy and fast way is to use templates for the application. There are many frameworks available in the market for making templates. In our case, we will take the example of the jade framework for templating.
Jade gets installed via the Node Package manager. This can be done by executing the following line in the command line
npm install jade
The above command requests the Node package manager to download the required jade modules and install them accordingly.
NOTE: In the latest version of Node jade has been deprecated. Instead, use pug.
Let’s use our newly installed jade framework and create some basic templates.
Step 1) The first step is to create a jade template. Create a file called index.jade and insert the below code. Ensure to create the file in “views” folder
var express=require('express');
var app=express();
app.set('view engine','jade');
app.get('/',function(req,res)
{
res.render('index',
{title:'Guru99',message:'Welcome'})
});
var server=app.listen(3000,function() {});
Code Explanation:
If the command is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Summary
#node-js #express #javascript #web-development