Pretty new to node/express, working on a weather app that takes in a location from a form to post
<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>
I'm able to get the data in the server code, but i need to transfer it to my app.get to use in my API call.
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);
});
You need to change your form method to GET
(or omit method attribute, GET
is default method). So when you submit your form request goes to this route
app.get("/", function(req, res) {
const cityName = req.query.city; // see http://expressjs.com/en/5x/api.html#req.query
const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=${apiKey}`;
..
code
..
res.render("index", weather_data);
});
This route app.post("/",...)
can be removed.
If I understood right, You want to send some sort of data to your server using a post request, then retrieve it using a get request right? In that case, you need a database in order to store your data at first. So you send your data to the server -> store it in the database -> retrieve data from database using a get request.
You can use both SQL or NoSQL databases. I suggest you use MongoDB with mongoose module.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.