Better error handling

This commit is contained in:
Simon Detheridge 2021-04-09 17:10:06 +01:00
parent 6960da6b45
commit 8064752200
Signed by: simon
GPG Key ID: 38640971DA1E704E

12
app.js
View File

@ -3,13 +3,19 @@ const { Resolver } = require('dns').promises
const app = express()
const r = new Resolver()
r.setServers(['1.1.1.1'])
app.get('/', async (req, res) => {
res.header('Content-type: text/plain')
const ip = req.connection.remoteAddress
let host
let ip, host
try {
host = await r.reverse(/.*:([^:]+)$/.exec(ip)[1])
ip = /.*:([^:]+)$/.exec(req.connection.remoteAddress)[1]
} catch(err) {
console.log(err.message)
ip = 'ERR'
}
try {
host = await r.reverse(ip)
} catch(err) {
console.log(err.message)
host = 'ERR'