-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (23 loc) · 879 Bytes
/
index.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
const express = require('express');
const app = express();
// Define the endpoint handler
app.get('/api/hello', (req, res) => {
const visitorName = req.query.visitor_name || 'Guest';
const clientIp = req.ip; // Gets the IP address of the requester
const location = 'New York'; // For simplicity, assume requester's location
// Simulate fetching temperature (can be extended with real API)
const temperature = 11;
// Construct the response object
const response = {
client_ip: clientIp,
location: location,
greeting: `Hello, ${visitorName}!, the temperature is ${temperature} degrees Celsius in ${location}`
};
// Send JSON response
res.json(response);
});
// Start the server
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});