-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
50 lines (42 loc) · 1.66 KB
/
Copy pathindex.mjs
File metadata and controls
50 lines (42 loc) · 1.66 KB
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
// Import the Express.js framework
import express from 'express';
// Import the Axios library for making HTTP requests
import axios from 'axios';
// Create a new Express.js app
const app = express();
// Set the port number for the server
const port = process.env.PORT || 3000;
// Use the Express.js static middleware to serve static files from the public directory
app.use(express.static('public'));
// Set the view engine to EJS (Embedded JavaScript)
app.set('view engine', 'ejs');
// Define a route for the /weather endpoint
app.get('/weather', async (req, res) => {
// Get the city name from the query string
const city = req.query.city;
// Set the API key for the OpenWeatherMap API
const apiKey = process.env.WEATHER_API_KEY || '607a5cd4fe2bfd5234cc5eabf0350a17';
// Construct the API URL for the OpenWeatherMap API
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
// Make a GET request to the OpenWeatherMap API
const response = await axios.get(apiUrl);
// Get the weather data from the response
const weatherData = response.data;
// Render the index.ejs template with the weather data
res.render('index', { weatherData });
} catch (error) {
// Handle any errors that occur during the API request
console.error(error);
res.status(500).send('Error fetching weather data');
}
});
// Define a route for the root endpoint
app.get('/', (req, res) => {
// Render the index.ejs template without any weather data
res.render('index');
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});