Node.JS - Express - Day Nine

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 
  1. var express_Obj = require('express')  
  2. var express = express_Obj();  
  3.   
  4. //routing  
  5. express.get('/'function (req, res) {  
  6. res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");  
  7. });  
  8.   
  9. //listener  
  10. 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
  1. var express_Obj = require('express')  
  2. var express = express_Obj();  
  3.   
  4. //routing  
  5. express.get('/'function (req, res) {  
  6. res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");  
  7. });  
  8.   
  9. express.get('/:id'function (req, res) {  
  10. res.send("Your Id is: "+req.params.id);  
  11. });  
  12.   
  13. express.get('/api/:name'function (req, res) {  
  14. res.send("Your Id is: " + req.params.name);  
  15. });  
  16. express.get('/:name/:id'function (req, res) {  
  17. res.send("Your Id is: " + req.params.id+ " and Your Name is : "+req.params.name);  
  18. });  
  19.   
  20. express.get('*'function (req, res) {  
  21. res.send("Sorry! URL not found");  
  22. });  
  23.   
  24. //listener  
  25. 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
  1. var express = require('express')  
  2. var routes = express.Router();  
  3.   
  4. //routing  
  5. routes.get('/'function (req, res) {  
  6. res.send("<html><head></head><body><h1>This is demo App</h1></body></html>");  
  7. });  
  8.   
  9. routes.get('/:id'function (req, res) {  
  10. res.send("Your Id is: "+req.params.id);  
  11. });  
  12.   
  13. routes.get('/api/:name'function (req, res) {  
  14. res.send("Your Id is: " + req.params.name);  
  15. });  
  16. routes.get('/:name/:id'function (req, res) {  
  17. res.send("Your Id is: " + req.params.id+ " and Your Name is : "+req.params.name);  
  18. });  
  19.   
  20. routes.get('*'function (req, res) {  
  21. res.send("Sorry! URL not found");  
  22. });  
  23.   
  24. module.exports = routes;  
App.js
  1. var express_Obj = require('express')  
  2. var express = express_Obj();  
  3. var routes = require('./routes.js');  
  4.   
  5. express.use('/', routes);  
  6. //listener  
  7. 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.
  1. var express_Obj = require('express')  
  2. var express = express_Obj();  
  3.   
  4. express.get('/:id([0-9]{1,4})'function (req, res) {  
  5. res.send("Your Id is: " + req.params.id);  
  6. });  
  7.   
  8. express.get('*'function (req, res) {  
  9. res.send("Sorry! URL not found");  
  10. });  
  11. //listener  
  12. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all