-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (24 loc) · 750 Bytes
/
Copy pathserver.js
File metadata and controls
31 lines (24 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
const PORT = process.env.PORT || 3000;
app.get("/api/ip/:ip", async (req, res) => {
const ip = req.params.ip;
try {
const response = await fetch(
`https://ipgeolocation.abstractapi.com/v1/?api_key=${process.env.ABSTRACT_API_KEY}&ip_address=${ip}`
);
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Backend Error:", error);
res.status(500).json({ error: "API request failed." });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});