Videos
What is the use of next in Express.js?
Next is a function in Express.js, which can be passed along with res and rep objects. It is used to pass control to the next middleware. There can be cases where we don’t want to complete the function in the current middleware and do the additional task in the next middleware. In these cases, next() is useful.
We will go through a simple example to understand the same. We will first create a new Node.js application by creating a directory and changing to it. Then will initialize a package.json file with the npm init -y command.
Next, we will install express in the app by giving the npm i express command.
Create a new file index.js in the root directory and add the below code in it. Here, we have two middleware, and we want to pass the control from the first to the second.
But notice in the first, we are not using the next function.
Now, run the server by node index.js command and go to the localhost:3000 from the browser. We can see that we only got the first message and not the second message from the second middleware.
Now, we will add next() in the first middleware and this is where the control will be passed to the second middleware.
Now, again run the server by node index.js command and go to the localhost:3000 from the browser. We can see the messages from both middleware and were able to pass the control from the first to the second one.
What is Express.js?
Express.js is a Node.js framework to develop the backend APIs of web and mobile application. It is Open San NPM package and is generally installed after creating a Node.js application.
Node.js itself is a JavaScript framework and is mainly used to develop backend APIs. But it can be used in socket programming to create chat apps. In fact, Node.js can even be used without Express.js to create backend APIs. But the code is very complicated and more difficult to implement.
This is where Express.js shines and does the one job perfectly for which it was created, which is, creating API endpoints. It adds the functionality of middleware and routing with ease in a Node.js application.
Some difference between Express.js and Node.js are -
- Express.js is built on top of Node.js and Node.js is built on top of Google’s V8 engine.
- Express.js was built on JavaScript and Node.js was built on JavaScript, C and C++.
- Routing is provided by default in Express.js but not provided in Node.js.
- Express.js requires less coding to write the same API backend, in comparison to Node.js.
In this instance, we will write the same app in Express.js and Node.js. First, we will write the app in Node.js. Here, we are using the createServer() method and are writing the response in a template language.
Now, we can go to http://localhost:3000/ and see the output.
Next, we will create the same app in Express.js and see the difference. We don’t need the template code for the entire page. Besides this, the code is much cleaner and easier to understand.
We can go to http://localhost:8080/ and we can see the result.
Why use Express.js?
This is a frequently asked interview question on express.js.
Express.js framework is created with JavaScript, and we code which we write with it is also written in JavaScript.Express.js. This is one of the reasons for the rise of MEAN and MERN stack, in which JavaScript knowledge can be used to learn Angular/React at Frontend, MongoDB in the Database and Node.js and Express.js at the backend.
Express has an in-built route support, so a developer can write responses to specific URLs with ease. We can also pass parameters to these routes, which add to the capabilities of the app. Express also supports multiple templating engines, through which you can generate static HTML for a web browser. If you follow this approach, the pages will be generated by the server, and there will be no need to write client code in Angular or React.
Without Express.js, we would have to write complex logic in Node.js, which is both tedious and time-consuming. Express.js code is minimalistic, efficient, and easily understandable by programmers, and because of it easily scalable.
Node.js programs are really fast because they implement event loops and get rid of all inefficiencies. With the power of Node.js and ease of an Express.js, we can build the backend for powerful and scalable web apps in no time.