Introduction to Express
Installing Express System-Wide
While there’s a convention of installing node apps inside the project, we can install express-generator because it automatically creates boilerplate code for express and node based applications.
The following line does the trick
Creating a new app in express
Open the directory where the express app needs to be present, and express again. This creates all the required code
Conventionally, the Express application is called app within our code, instantiated by:
The middleware is any function which changes the way request and response objects are changed before, in, or after, the request and the response. Usually this is called the middleware stack, because the order in which middleware functions are defined is the order in which they are executed.
They have the same syntax except the presence of a parameter called next which calls the next Middleware in line.
Let’s take an example, where the requests coming at the root are handled by some function:
In order to introduce middleware, we have to add another function, which is the middleware:
There are two important points to notice here:
The order is of paramount importance. Only because the middleware came before the route handling function, the latter could use the modified request.
The parameter next, which is a method and is called just before the function returns. Unless next is called, the request will be left hanging.
The middleware methods can be called before as well as after the routing. Again, we have to make sure that even the routing function calls the next method in those situations.
Of course, just like the routing methods, even the middleware can take some routes and then will only modified those requests which match the given route.
For instance, the following middleware will only modify the route starting with /users
What we have been talking about is the app level middleware, which controls the routing at the application level. In order make the application more modular, and convenient, it’s recommended to divide the routing based on different routes.
Let’s say we have a section of the application under the route /users. In order to use it, we first have to have a separate routing file, let’s call it users.js and place it under routes directory.
Then from the main app, we can access it using:
In the file users.js, we can have the routing logic:
Of course, we have the option of using middlewares in the routing files too.
Author Tushar Tyagi
LastMod Feb 21, 2015