I'm trying to get url parameters in express 4.17.3 using the urlencoded middleware but the body is always empty, I reproducted it to a minimal code:
const express = require("express");
(()=>{
const app = express();
app.use(express.urlencoded());
app.get("/", async(req, res)=>{
console.log(req.body); //always print '{}'
res.send();
});
app.listen(83, ()=>{
console.log("test app listening on port 83");
})
})();
Here's my request
http://localhost:83/?param1=42
What am I doing wrong?
A few things to break down. To answer your question to get params you can use
req.query
so your code would be:Addressing
urlencoded
it should be changed from:to:
good habit to not hard code the
port
so the line of code:should be:
full working code example:
When using express I also like to implement
nodemon
and if you add this to your package.json:then in the terminal you can run:
you wont have to restart your server during development changes.