Home > other >  Express app req.body is empty on form submit
Express app req.body is empty on form submit

Time:01-17

Why am I getting an empty req.body here? I have looked through the other answers and I am using body-parser as they say.

Form:

  <form action="/" method="post">
    <input name="input" placeholder="Shorten a link here..."/>
    <button  type="submit">Shorten It!</button>
  </form>

Server:

...
app.use(express.json())
app.use(cors())
...
app.post("/", (req, res) => {
  console.log("req.body :>> ", req.body)
})

CodePudding user response:

The form submits a url-encoded body. You should use the urlencoded middleware to parse it:

app.use(express.urlencoded({extended: false}));
  • Related