Hola, estoy tratando de publicar 4 valores clave (weatherDescription, query, temp, imageURL) en un archivo success.ejs. Pero estos valores están dentro de una declaración if. Actualmente, solo se envían a un documento HTML en blanco y quiero CSS en la página de éxito. ¡Cualquier consejo para lograr esto sería muy apreciado!
app.post("/", function(req, res){ const query = req.body.cityName; const unit = "metric"; const apiKey = "********"; const url = "https://api.openweathermap.org/data/2.5/weather?appid=" + apiKey + "&q=" + query + "&units=" + unit; https.get(url, function(response){ console.log(response.statusCode); if (response.statusCode === 200) { response.on("data", function(data){ const weatherData = JSON.parse(data); const temp = weatherData.main.temp; const weatherDescription = weatherData.weather[0].description; const icon = weatherData.weather[0].icon; const imageURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png"; res.write("<p>The weather is currently " + weatherDescription + " </p>"); res.write("<h1>The temperature in " + query + " is " + temp + " degrees Celcius.</h1>"); res.write("<img src=" + imageURL + ">"); res.send(); }); } else { res.sendFile(__dirname + "/failure.html"); } }); }); app.post("/success", function(req, res){ res.redirect("/success"); }); app.post("/failure", function(req, res) { res.redirect("/"); }); In success.ejs: <p><%= weatherDescription %></p> <p><%= query %></p> <p><%= temp %></p> <p><%= imageURL %></p>Puede enviar fácilmente contenido a un archivo EJS utilizando los parámetros de res.render. Por ejemplo, puede llamar a res.render con estos parámetros:
res.render( path.join(__dirname, "success.ejs"), { weatherDescription : weatherDescription, query : query, temp : temp, imageURL : imageURL } );Y acceder a ellos desde EJS a través de:
<p>The weather is currently <%= weatherDescription %></p> <h1>The temperature in <%= query %> is <%= temp %> degrees Celcius.</h1> <img src="<%= imageURL %>">Si esto ayudó, márquelo como una respuesta. (probablemente no)