-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (46 loc) · 1.7 KB
/
server.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require("dotenv").config()
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.json());
const OPENWEATHERMAP_API_KEY =
process.env.OPENWEATHERMAP_API_KEY ;
app.get("/api/hello", async (req, res) => {
const visitorName = req.query.visitor_name || "Visitor";
// Attempt to get the client's IP address, considering Vercel's proxy setup
const visitorIp =
req.headers["x-forwarded-for"] || req.connection.remoteAddress;
try {
// Fetch the location of the visitor using ipwhois.app
const locationResponse = await axios.get(
`https://ipwhois.app/json/${visitorIp}`
);
// Extract city and country
const { city, country } = locationResponse.data;
if (!city || !country) {
throw new Error("City or country not found in location response");
}
// Fetch weather using OpenWeatherMap
const weatherResponse = await axios.get(
`https://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${OPENWEATHERMAP_API_KEY}&units=metric`
);
// Get temperature information
const temperature = weatherResponse.data.main.temp;
// Construct greetings message using the given format
const greetings = `Hello, ${visitorName}! The temperature is ${temperature}°C in ${city}, ${country}.`;
const responseData = {
client_ip: visitorIp,
location: `${city},${country}`,
greetings: greetings,
};
res.json(responseData);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});