Skip to content

Commit 506a52c

Browse files
committed
Added log
1 parent 69533e2 commit 506a52c

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

lib/wrapper.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import express from "express";
22
import axios from "axios";
3+
import chalk from "chalk";
4+
5+
const methodColors = {
6+
"POST": chalk.yellow,
7+
"GET": chalk.green,
8+
"DELETE": chalk.red,
9+
"PATCH": chalk.blue,
10+
"PUT": chalk.magenta,
11+
"HEAD": chalk.cyan,
12+
"OPTIONS": chalk.gray,
13+
"TRACE": chalk.white,
14+
"CONNECT": chalk.black,
15+
};
316

417
const Wrapper = (port = 3000) => {
518
const app = express();
619

720
app.use(express.json());
821

22+
// Middleware for logging
23+
app.use((req, res, next) => {
24+
const { url, method } = req.body;
25+
26+
// Log request method and URL with colors
27+
const colorizeMethod = methodColors[method] || chalk.reset;
28+
console.log(colorizeMethod(`${method} ${url}`));
29+
30+
next();
31+
});
32+
933
app.post("/api/request", async (req, res) => {
1034
try {
1135
const { url, method, headers, params, data } = req.body;
@@ -20,13 +44,12 @@ const Wrapper = (port = 3000) => {
2044

2145
res.json(response.data);
2246
} catch (error) {
23-
console.error("Error:", error.response.data);
2447
res.status(error.response.status).json({ error: error.message });
2548
}
2649
});
2750

2851
app.listen(port, () => {
29-
console.log(`Wrapper is running on http://localhost:${port}`);
52+
console.log(`Wrapper is running on ${chalk.blue(`http://localhost:${port}`)}\n`);
3053
});
3154
};
3255

0 commit comments

Comments
 (0)