-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathweb.js
More file actions
55 lines (48 loc) · 1.56 KB
/
web.js
File metadata and controls
55 lines (48 loc) · 1.56 KB
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
const express = require('express');
const app = express();
/**
* 1. Proxy Configuration
* Enabling 'trust proxy' allows Express to automatically parse the
* 'x-forwarded-for' header when the app is behind a load balancer (Heroku, Nginx, etc.)
*/
app.set('trust proxy', true);
/**
* 2. IP Retrieval Utility
* Fallback logic to ensure we get an IP even if 'trust proxy' isn't used.
*/
const getIp = (req) => req.ip || req.socket.remoteAddress;
// Root Route: Returns only the client's IP address
app.get('/', (req, res) => {
res.send(getIp(req));
});
// API Route: Returns detailed request metadata in JSON format
app.get('/api/ipaddress.json', (req, res) => {
res.json({
userAgent: req.headers['user-agent'],
method: req.method,
fresh: req.fresh,
xhr: req.xhr,
protocol: req.protocol,
ipAddress: getIp(req),
// Included for debugging purposes
forwarded: req.headers['x-forwarded-for'] || null
});
});
/**
* 3. Server Public IP Logger
* Fetches the server's own public IP address using the native fetch API (Node.js 18+)
*/
const logServerPublicIp = async () => {
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
console.log(`>>> Server Public IP: ${data.ip}`);
} catch (err) {
console.error("Could not retrieve server public IP:", err.message);
}
};
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
logServerPublicIp();
});