Skip to content

Commit a7033c5

Browse files
authored
Merge pull request #4 from fac31/logger
Logger
2 parents 3302ae2 + 33fcf89 commit a7033c5

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
# reza-todd-final
22

3-
how to get started
4-
`git clone `
3+
how to get started:
4+
`git clone [email protected]:fac31/reza-todd-final.git`
55

6+
navigate to the directory:
67
`cd reza-todd-final`
78

9+
create a `.env` file in the root directory and add the following:
10+
11+
`PORT=your_port_number`
12+
13+
install dependencies
814
`pnpm install`
915

16+
run the app
1017
`pnpm start`
1118

19+
run the app in dev mode
20+
`pnpm dev`
21+
22+
run the tests
1223
`pnpm test`
1324

14-
`pnpm run test`

middlewares/logger.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const logger = (req, res, next) => {
2+
const colorMethods = {
3+
GET: "green",
4+
POST: "yellow",
5+
PUT: "blue",
6+
DELETE: "red",
7+
};
8+
const color = colorMethods[req.method] || "white";
9+
10+
console.log(
11+
`${req.method} ${req.protocol}://${req.get("host")}${req.originalUrl}`[
12+
color
13+
]
14+
);
15+
next();
16+
};
17+
18+
module.exports = logger;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15+
"colors": "^1.4.0",
1516
"dotenv": "^16.4.5",
1617
"express": "^4.19.2"
1718
},

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const express = require("express");
22
const path = require("path");
33
require("dotenv").config();
4-
4+
const logger = require("./middlewares/logger");
55
const app = express();
66
const PORT = process.env.PORT || 3000;
77

@@ -11,10 +11,11 @@ app.enable("trust proxy");
1111
// Middleware
1212
app.use(express.static(path.join(__dirname, "public")));
1313
app.use(express.json());
14+
app.use(logger);
1415

15-
app.get('/', (req, res) => {
16-
// res.status(200).sendFile(path.join(__dirname, "public", "index.html"));
17-
res.status(200).send("Hello World");
16+
app.get("/", logger, async (req, res) => {
17+
// res.status(200).sendFile(path.join(__dirname, "public", "index.html"));
18+
res.status(200).send("Hello World");
1819
});
1920

2021
app.listen(PORT, () =>

0 commit comments

Comments
 (0)