Last active
December 11, 2017 01:21
-
-
Save idoco/d9e79f026f6f6f61edd73584863eba94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const path = require("path"); | |
const geoip = require('geoip-lite'); | |
const Loadmill = require('express-loadmill'); | |
const app = express(); | |
app.use(Loadmill({verifyToken: process.env.LOADMILL_VERIFY_TOKEN})); | |
app.get('/location', (req, res) => { | |
try { | |
let ip = req.headers['x-forwarded-for'] || ""; | |
ip = ip.split(",")[0] || "0.0.0.0"; | |
const location = geoip.lookup(ip) || {}; | |
location.ip = ip; | |
console.log("Location is ", location); | |
res.setHeader('Content-Type', 'application/json'); | |
res.send(JSON.stringify(location)); | |
} catch (e) { | |
console.log("error: ", e); | |
res.status(500).end(); | |
} | |
}); | |
app.get('/', (req, res) => { | |
res.sendFile(path.join(__dirname+'/index.html')); | |
}); | |
app.listen(3000, () => console.log('Example app listening on port 3000!')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment