2022-04-23 13:26:53 +01:00
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- node-js
|
2022-04-29 07:30:47 +01:00
|
|
|
- middleware
|
2022-04-23 13:26:53 +01:00
|
|
|
---
|
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
# Middleware
|
|
|
|
|
2022-04-29 07:30:47 +01:00
|
|
|
## What is middleware?
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- Anything that terminates the `req, res` cycle counts as middleware. It is
|
|
|
|
basically anything that acts as an intermediary once the request is received
|
|
|
|
but before the resource is sent. A good example would be the
|
|
|
|
`app.use(express.json()` or `app.use(bodyParser.json)` functions we call in
|
|
|
|
order to be able to parse JSON that is sent from the client.
|
|
|
|
- You will most likely have multiple middleware functions running at once. We
|
|
|
|
call this intermediary part of the cycle the **request processing pipeline**.
|
|
|
|
- Generally all middleware will be added as a property on the Express `app`
|
|
|
|
instance with the `app.use(...)` syntax.
|
2022-04-23 13:26:53 +01:00
|
|
|
|
2022-04-29 07:30:47 +01:00
|
|
|
## Creating custom middleware functions
|
2022-04-23 13:26:53 +01:00
|
|
|
|
|
|
|
### Basic schema
|
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
```js
|
2022-04-23 13:26:53 +01:00
|
|
|
app.use((req, res, next) => {
|
2022-09-06 15:44:40 +01:00
|
|
|
// do some middleware
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
```
|
2022-04-23 13:26:53 +01:00
|
|
|
|
|
|
|
### `next`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
The `next` parameter is key, it allows Express to move onto the next middleware
|
|
|
|
function once the custom middleware executes. Without it, the request processing
|
|
|
|
pipeline will get blocked.
|
2022-07-14 08:00:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Middleware functions are basically asynchronous requests and as such they use a
|
|
|
|
similar syntax as Promises (e.g `then`) for sequencing processes.
|
2022-04-23 13:26:53 +01:00
|
|
|
|
|
|
|
### Example of sequence
|
|
|
|
|
2022-07-14 08:00:04 +01:00
|
|
|
```js
|
2022-04-23 13:26:53 +01:00
|
|
|
app.use((req, res, next) => {
|
2024-02-02 15:58:13 +00:00
|
|
|
console.log("Do process A...");
|
2022-09-06 15:44:40 +01:00
|
|
|
next();
|
|
|
|
});
|
2022-04-23 13:26:53 +01:00
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
2024-02-02 15:58:13 +00:00
|
|
|
console.log("Do process B...");
|
2022-09-06 15:44:40 +01:00
|
|
|
next();
|
|
|
|
});
|
2022-04-27 07:30:50 +01:00
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> It makes more sense of course to define our middleware within a function and
|
|
|
|
> then pass it as an argument to `app.use()`
|
2022-04-27 07:30:50 +01:00
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
## Including middleware based on environment
|
|
|
|
|
2022-04-27 08:00:43 +01:00
|
|
|
With a full-scale Node application you will typically run three environments:
|
2022-09-06 15:44:40 +01:00
|
|
|
|
|
|
|
- Development
|
|
|
|
- Testing
|
|
|
|
- Production
|
2022-04-29 07:30:47 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We will not want to run certain types of middleware in all environments. For
|
|
|
|
example, it would be costly to run logging in the app's production environment.
|
|
|
|
It would make more sense to run this only in development.
|
2022-04-29 07:30:47 +01:00
|
|
|
|
|
|
|
### Accessing current Node environment
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We can control which middleware we run via the Node envrionment variables:
|
2024-06-16 18:30:03 +01:00
|
|
|
`process.env` (see for instance [ports](Ports_in_NodeJS.md)).
|
2022-04-29 07:30:47 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We could set
|
2024-02-17 11:57:44 +00:00
|
|
|
[Morgan](Morgan.md) to run
|
2024-02-02 15:58:13 +00:00
|
|
|
only in development with:
|
2022-04-29 07:30:47 +01:00
|
|
|
|
|
|
|
```js
|
2024-02-02 15:58:13 +00:00
|
|
|
if (app.get("env") === "development") {
|
|
|
|
app.use(morgan("common"));
|
|
|
|
console.log("Morgan enabled");
|
2022-04-29 07:30:47 +01:00
|
|
|
}
|
|
|
|
```
|