-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (46 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require("express");
const morganBody = require("morgan-body");
const routes = require("./routes");
const fs = require("fs");
const PORT = process.env.PORT || 5000;
// Clear the log file when the application starts
fs.writeFileSync("request-response-log.txt", "");
const app = express().use(express.json());
// // Middleware to log request body and corresponding response
// app.use((req, res, next) => {
// let oldWrite = res.write,
// oldEnd = res.end;
// let chunks = [];
// res.write = function (chunk) {
// chunks.push(chunk);
// oldWrite.apply(res, arguments);
// };
// res.end = function (chunk) {
// if (chunk) chunks.push(chunk);
// let body = Buffer.concat(chunks).toString("utf8");
// // Log request and response
// fs.appendFileSync(
// "request-response-log.txt",
// `Request Body:\n${JSON.stringify(
// req.body
// )}\nResponse Body:\n${body}\n---\n`
// );
// oldEnd.apply(res, arguments);
// };
// next(); // Proceed to the next middleware
// });
// Use routes
app.use("/", routes);
app.get("/", (req, res) => res.send("Hello World!"));
// GET request handler for /chinese-wall
app.get('/chinese-wall', (req, res) => {
const response = {
"1": "Fluffy",
"2": "Galactic",
"3": "Mangoes",
"4": "password4",
"5": "password5"
};
res.json(response);
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));