* Expanded the solution for the 'Get Route Parameter Input from the Client' challenge * Expanded the guide for the 'Use body-parser to Parse POST Requests' challenge * Rewritten guide for the 'Serve JSON on a Specific Route' challenge and fixed source link * Expanded the guide for the 'Serve Static Assets' challenge * Expanded solution to the 'Get Query Parameter Input from the Client' challenge and fixed links to source file * Added solution to the 'Chain Middleware to Create a Time Server' challenge and fixed link to source file * Rewrite the 'Start a Working Express Server' challenge * Expanded the guide for 'Expand Your Project with External Packages from npm' * Added reference to semantic versioning in 'Add a Version to Your package.json' * fix/remove-links+fix-solutions * fix/remove-more-links
29 lines
865 B
Markdown
29 lines
865 B
Markdown
---
|
|
title: Implement a Root-Level Request Logger Middleware
|
|
---
|
|
## Implement a Root-Level Request Logger Middleware
|
|
|
|
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
|
|
|
It is easier to write this challenge all at the top (there is already a stub for it). This is because middleware must be placed the function calls you want it to be used for.
|
|
|
|
To set up your own middleware you can do it like so:
|
|
|
|
```javascript
|
|
app.use(function middleware(req, res, next) {
|
|
// Do something
|
|
// Call the next function in line:
|
|
next();
|
|
});
|
|
|
|
```
|
|
|
|
If you have trouble formatting the string correctly, one way to do it looks like:
|
|
|
|
```javascript
|
|
var string = req.method + ' ' + req.path + ' - ' + req.ip;
|
|
```
|
|
|
|
### Resources
|
|
- [Express Middleware](https://expressjs.com/en/guide/using-middleware.html)
|