Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
63 lines (53 loc) · 1.57 KB
/
Copy pathserver.mjs
File metadata and controls
63 lines (53 loc) · 1.57 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
56
57
58
59
60
61
62
63
/*
* A minimal Express app using @ipregistry/express.
*
* Run with:
* IPREGISTRY_API_KEY=YOUR_API_KEY node server.mjs
*
* Then try:
* curl http://localhost:3000/
* curl http://localhost:3000/geo
*/
import express from 'express'
import {
blockCountries,
blockThreats,
ipregistry,
isEuVisitor,
} from '@ipregistry/express'
const app = express()
// Match your proxy topology so req.ip reflects the real client:
// https://expressjs.com/en/guide/behind-proxies.html
app.set('trust proxy', 1)
app.use(
ipregistry({
fields: 'ip,location,security',
// On localhost your IP is private, so no lookup happens. Use a
// fixed public IP in development to exercise geo features:
developmentIp:
process.env.NODE_ENV !== 'production' ? '66.165.2.7' : undefined,
}),
)
// Composable guards (both fail open when data is unavailable):
app.use(blockCountries({ countries: ['KP', 'IR'] }))
app.use(blockThreats({ tor: true, vpn: true }))
app.get('/', (req, res) => {
const { data } = req.ipregistry
res.send(
`Hello ${data?.location?.country?.name ?? 'visitor'}!` +
(isEuVisitor(req.ipregistry) ? ' (GDPR consent required)' : ''),
)
})
app.get('/geo', (req, res) => {
const { ip, data, skipped, error } = req.ipregistry
res.json({
ip,
country: data?.location?.country?.code ?? null,
city: data?.location?.city ?? null,
skipped: skipped ?? null,
error: error ?? null,
})
})
app.listen(3000, () => {
console.log('Listening on http://localhost:3000')
})