2022-07-12 19:30:03 +01:00
|
|
|
---
|
2022-09-06 15:44:40 +01:00
|
|
|
categories:
|
|
|
|
- Programming Languages
|
2022-07-12 19:30:03 +01:00
|
|
|
tags:
|
|
|
|
- backend
|
|
|
|
- node-js
|
|
|
|
- middleware
|
|
|
|
---
|
|
|
|
|
|
|
|
# Morgan
|
|
|
|
|
2022-09-06 15:44:40 +01:00
|
|
|
Morgan is middleware that is used to log HTTP requests to the Express instance.
|
2022-07-12 19:30:03 +01:00
|
|
|
|
|
|
|
```js
|
2024-02-02 15:58:13 +00:00
|
|
|
app.use(morgan("dev"));
|
2022-07-12 19:30:03 +01:00
|
|
|
```
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
With Morgan in place, every time we run a request it will be logged on the
|
|
|
|
console that is running our Node application, e.g:
|
2022-07-12 19:30:03 +01:00
|
|
|
|
|
|
|
```plain
|
|
|
|
GET /api/courses 200 95 - 1.774 ms
|
|
|
|
```
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This uses the `tiny` default which logs the bare minimum giving us: request
|
|
|
|
type; endpoint; response code; and time to execute. But there are more verbose
|
|
|
|
settings.
|
2022-07-12 19:30:03 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
It defaults to logging on the console but can also be configured to write to a
|
|
|
|
log file.
|