Skip to content

Instantly share code, notes, and snippets.

@idoco
Last active December 11, 2017 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idoco/d9e79f026f6f6f61edd73584863eba94 to your computer and use it in GitHub Desktop.
Save idoco/d9e79f026f6f6f61edd73584863eba94 to your computer and use it in GitHub Desktop.
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