Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Cómo enviar una solicitud de publicación con 4 valores clave que están en una declaración If, dentro de otra declaración de publicación

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>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!