【发布时间】:2019-09-17 22:30:11
【问题描述】:
node/express 的新手,正在开发一个天气应用程序,该应用程序从表单中获取位置并发布
<form method="POST" action="/">
<input id="input" type="text" name="city" placeholder="Search by name or zip code" />
<button id="button" type="submit"></button>
</form>
我能够在服务器代码中获取数据,但我需要将其传输到我的 app.get 以在我的 API 调用中使用。
app.post("/", function(req, res) {
let input = req.body.city;
console.log(input);
});
app.get("/", function(req, res) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=${apiKey}`;
..
code
..
res.render("index", weather_data);
});
【问题讨论】:
-
从 get 映射中提取逻辑到一个接受 cityName 作为参数的方法中。从 get 和 post 调用该方法。
-
我不知道为什么快速程序员会忘记它只是 Javascript,并且您可以将通用代码分解为从多个路由调用的函数。但是,这就是你可以在这里做的。
标签: javascript node.js express post get