您好,我正在嘗試將 4 個關鍵值(weatherDescription、query、temp、imageURL)發布到 success.ejs 檔案。但是這些值在 if 陳述句中。目前,它們只是被發送到一個空白的 HTML 檔案,我想對成功頁面進行 CSS。任何實作此目的的提示將不勝感激!
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>
uj5u.com熱心網友回復:
您可以使用 res.render 的引數輕松地將內容發送到 EJS 檔案。例如,您可以使用以下引數呼叫 res.render:
res.render( path.join(__dirname, "success.ejs"), { weatherDescription : weatherDescription, query : query, temp : temp, imageURL : imageURL } );
并通過以下方式從 EJS 訪問它們:
<p>The weather is currently <%= weatherDescription %></p>
<h1>The temperature in <%= query %> is <%= temp %> degrees Celcius.</h1>
<img src="<%= imageURL %>">
如果這有幫助,請將其標記為答案。(它可能沒有)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360660.html
標籤:javascript
