Files
stream.graff.tech-server/src/routes/getCountryCode.ts
T
2024-06-06 15:14:23 +05:00

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;