forked from jpillora/node-echo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (33 loc) · 926 Bytes
/
server.js
File metadata and controls
38 lines (33 loc) · 926 Bytes
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
const express = require("express");
const app = express();
const port = process.env.PORT || 3000; // You can change this to any port you prefer
// Middleware to parse JSON and URL-encoded data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Middleware to log request details
app.use((req, res, next) => {
const timestamp = new Date().toISOString();
const method = req.method;
const url = req.url;
const headers = req.headers;
const host = req.headers.host;
const body = req.body;
const ip = req.ip;
// Log request details
console.log(`[${timestamp}] ${method} ${url} - Headers:`, headers);
// Pass control to the next middleware
res.json({
message: "Received",
timestamp,
ip,
method,
host,
url,
body,
headers,
});
});
// Start the server
app.listen(port, () => {
console.log(`Echo server is running on http://localhost:${port}`);
});