-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
42 lines (32 loc) · 1.32 KB
/
app.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
const axios = require('axios');
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Add this line after app initialization
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index', { error: null, location: null });
});
app.post('/location', async (req, res) => {
const { location } = req.body;
if (!location) {
return res.render('index', { error: 'Location is required in the request body', location: null });
}
const apiUrl = `https://api.weatherapi.com/v1/current.json?key=c3961c028a3640cfad7132456230212&q=${location}&aqi=yes`;
try {
const response = await axios.get(apiUrl);
const data = response.data;
console.log(`Location : ${data.location.name}\nCondition : ${data.current.condition.text}\nTemperature : ${data.current.temp_c}`);
// Render the EJS file with the weather information
res.render('index', { error: null, location: data.current ,place: data.location, png:data.current.condition.icon});
} catch (error) {
console.error('Error fetching data:', error);
res.render('index', { error: 'Internal Server Error', location: null });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});