Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Express provides a thin layer of fundamental web application features, without obscuring Node.js features. Express provides a platform to develop the web applications in a better and well maintained way. It also contains pre-built modules that can be used in our application. In this article, we will learn about Express.
Install the Express
To install the Express framework, open your command line tool and write the “npm install express –save” command. This command downloads all the files required for Express in your project.
When you open your project structure, you will find that a new directory “node_modules” has been added and this directory contains a sub directory named as “express”. It indicates that the Express dependencies has been installed successfully in your project.
Example 1
- var express_Obj = require('express')
- var express = express_Obj();
-
-
- express.get('/', function (req, res) {
- res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");
- });
-
-
- express.listen(1400);
In this example, we use the “express” module using “require” method. In the next line, we create a listener for “get” HTTP request. For this request, we create a callback function that contains two parameters “req” and “res”. “res” object contains all the information related to HTTP request and using “res” object, we send the response of HTTP request. In the last line, we define the PORT number. Listen function is used to create a connection for a port and specific HOST name.
After all this setup, when you run this app and open “localhost:1400” address in your browser, you will get the below output.
Routing
Routing is a process for defining which page will be hit or what data will be sent to the user, according to the incoming request. Several factors are used to define the routing, like request method (GET, POST, DELETE), parameter values etc. Let’s take an example.
Example
- var express_Obj = require('express')
- var express = express_Obj();
-
-
- express.get('/', function (req, res) {
- res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");
- });
-
- express.get('/:id', function (req, res) {
- res.send("Your Id is: "+req.params.id);
- });
-
- express.get('/api/:name', function (req, res) {
- res.send("Your Id is: " + req.params.name);
- });
- express.get('/:name/:id', function (req, res) {
- res.send("Your Id is: " + req.params.id+ " and Your Name is : "+req.params.name);
- });
-
- express.get('*', function (req, res) {
- res.send("Sorry! URL not found");
- });
-
-
- express.listen(1400);
Output
In this example, we are showing our routes in Index.html page; that is a tedious method. But, we also write our all routes in a separate file and use that file in index.html page. Create a new JavaScript file and name it as “routes.js”. In this file, we can define our routes.
Routes.js
- var express = require('express')
- var routes = express.Router();
-
-
- routes.get('/', function (req, res) {
- res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");
- });
-
- routes.get('/:id', function (req, res) {
- res.send("Your Id is: "+req.params.id);
- });
-
- routes.get('/api/:name', function (req, res) {
- res.send("Your Id is: " + req.params.name);
- });
- routes.get('/:name/:id', function (req, res) {
- res.send("Your Id is: " + req.params.id+ " and Your Name is : "+req.params.name);
- });
-
- routes.get('*', function (req, res) {
- res.send("Sorry! URL not found");
- });
-
- module.exports = routes;
App.js
- var express_Obj = require('express')
- var express = express_Obj();
- var routes = require('./routes.js');
-
- express.use('/', routes);
-
- express.listen(1400);
Output
Example
We can also use the regular expression in routing. For example, if you want to process only those requests for which id is numeric type and maximum length of id is 4, then use the following code.
- var express_Obj = require('express')
- var express = express_Obj();
-
- express.get('/:id([0-9]{1,4})', function (req, res) {
- res.send("Your Id is: " + req.params.id);
- });
-
- express.get('*', function (req, res) {
- res.send("Sorry! URL not found");
- });
-
- express.listen(1400);
Output
The above program provides the same result as the previous example but with a little difference that in this example, we don’t define the routes in app.js file. We defined the routes in another file (routes.js) and after that, used that file in our main file (app.js).
Install Nodemon
Without Nodemon, if we make any changes in the code, then we are required to stop and start the server again. Well, that is a very tedious and boring task. But thanks to “npm” that provides Nodemon. Using Nodemon, we don’t have a requirement to stop and start the server again for any changes. Whenever we make any change in the code ,“Nodemon” automatically restarts the server, which saves our time and efforts.
To install the “npm”, run “npm install -g nodemon” command.
After successful installation of Nodemon, you can run any Node.js application using the “nodemon” command.
“Nodemon” starts your server. Whenever you make any changes in the code, it will restart the server automatically. So, we don’t need to stop and start the server manually.
Conclusion
In this article, we got a brief introduction of Express framework. From my next article, we will cover some advanced topics. Thanks for reading this article.