30 lines
619 B
TypeScript
30 lines
619 B
TypeScript
import { Router } from "express";
|
|
import got from "got-cjs";
|
|
|
|
const router = Router();
|
|
|
|
router.get("/", async (req, res) => {
|
|
const ip = req.headers["x-real-ip"];
|
|
|
|
if (!ip)
|
|
return res.json({
|
|
error: "An error occurred while obtaining an IP address",
|
|
});
|
|
|
|
try {
|
|
const { countryCode }: { countryCode: string } = await got
|
|
.get(`http://ip-api.com/json/${ip}`)
|
|
.json();
|
|
|
|
res.json({ countryCode });
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
res.json({ error: error.message });
|
|
}
|
|
}
|
|
});
|
|
|
|
const getCountryCodeRouter = router;
|
|
|
|
export default getCountryCodeRouter;
|