-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.14 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
const express = require('express');
const ipinfo = require('ipinfo');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static('public'));
app.use((req, res, next) => {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
ipinfo(ip, (err, data) => {
if (err) {
console.error('Error fetching IP details:', err.message);
res.status(500).json({ error: 'Failed to retrieve IP details' });
} else {
req.ipDetails = data;
next();
}
});
});
app.get('/api/ipdetails', (req, res) => {
const timestamp = new Date().toISOString();
const ipDetails = {
timestamp,
ip: req.ipDetails.ip || 'N/A',
city: req.ipDetails.city || 'N/A',
region: req.ipDetails.region || 'N/A',
country: req.ipDetails.country || 'N/A',
loc: req.ipDetails.loc || 'N/A',
org: req.ipDetails.org || 'N/A',
timezone: req.ipDetails.timezone || 'N/A',
isp: req.ipDetails.isp || 'N/A',
ipv6: req.ipDetails.ipv6 || 'N/A',
ipv4: req.ipDetails.ipv4 || 'N/A',
};
res.json(ipDetails);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});