* 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.
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. middleware functions are basically asynchronous requests and as such they use a similar syntax as Promises (e.g `then`) for sequencing processes.
Let's say we have a file called `something.txt` that resides at `public/something.txt`
We can expose this to express with `app.use(static('public'))`. Then if we navigate to `localhost:3000/readme.txt` the file will be served in the browser. (Not the `public` subdirectory is not included in the URL, it will be served from root).
Generally we handle the data of API requests via a JSON body and the `express.json()` middleware. However, in cases where the data is sent from the client in the form of `key=value&key=value` appendages to the request URL, `urlencoded` allows us to parse them.
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.
### Accessing current Node environment
We can control which middleware we run via the Node envrionment variables: `process.env` (see for instance [ports](./Ports.md)).
To determine the current environment we can use the variable `process.env.NODE_ENV`. This works globally regardless of the kind of Node app we are building. But in Express, there is a built in method for retrieving the current envrionment: `app.get('env')`.
If you haven't manually set up your environments Node will return `undefined` but express defaults to `development`.
```js
console.log(process.env.NODE_ENV); // undefined
console.log(app.get("env")); // development
```
###
We can set Morgan to run only in development with: