-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (89 loc) · 3.28 KB
/
app.js
File metadata and controls
120 lines (89 loc) · 3.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// vars
const express = require('express');
const app = express();
const port = 7777;
const posts = [];
const axios = require('axios');
const apiKey = '96eaab2fdda081abcd3e9e54c043fd7c';
// code to check form data
app.use(express.urlencoded({ extended: true }));
// Stuff for the static files
app.use(express.static('public'));
app.set('view engine', 'ejs');
// routes below
app.get('/', (req, res) => {
res.render('index', { posts });
});
// create the post and display it
app.post('/create', async (req, res) => {
const { author, title, content, city } = req.body;
const date = new Date().toLocaleString();
// adds weather
let weather = 'Weather unavailable';
// get weather
try {
// get lat and long with geocoding api
const geoUrl =
'http://api.openweathermap.org/geo/1.0/direct?q=' +
encodeURIComponent(city) +
'&limit=1&appid=' + apiKey;
const geoResponse = await axios.get(geoUrl);
const geoData = geoResponse.data;
if (geoData.length === 0) throw new Error('Location not found');
const { lat, lon } = geoData[0];
// get weather from weather api
const weatherUrl =
'https://api.openweathermap.org/data/2.5/weather?lat=' +
lat + '&lon=' + lon +
'&units=imperial&appid=' + apiKey;
const weatherResponse = await axios.get(weatherUrl);
const weatherData = weatherResponse.data;
weather = `${weatherData.weather[0].description},
${weatherData.main.temp}°F`;
}
// give error if you cant get the weather
catch (error) {
console.error('Failed to fetch weather:', error.message);
}
posts.push({ author, title, content, date, weather });
res.redirect('/');
});
// edit the post
app.get('/edit/:index', (req, res) => {
const index = req.params.index;
const post = posts[index];
res.render('edit', { post, index });
});
// posting the edit
app.post('/edit/:index', async (req, res) => {
const index = req.params.index;
const { author, title, content, city } = req.body;
const date = new Date().toLocaleString();
let weather = 'Weather unavailable';
try {
const geoUrl = `http://api.openweathermap.org/geo/1.0/direct?q=${
encodeURIComponent(city)}&limit=1&appid=${apiKey}`;
const geoResponse = await axios.get(geoUrl);
const geoData = geoResponse.data;
if (geoData.length === 0) throw new Error('Location not found');
const { lat, lon } = geoData[0];
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${
lat}&lon=${lon}&units=imperial&appid=${apiKey}`;
const weatherResponse = await axios.get(weatherUrl);
const weatherData = weatherResponse.data;
weather = `${weatherData.weather[0].description}, ${weatherData.main.temp}°F`;
} catch (error) {
console.error('Failed to fetch weather during edit:', error.message);
}
posts[index] = { author, title, content, date, city, weather };
res.redirect('/');
});
// updating post display when one is deleted
app.post('/delete/:index', (req, res) => {
posts.splice(req.params.index, 1);
res.redirect('/');
});
// used for testing
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});