George Garman
George Garman

Reputation: 91

Express app req.body is empty on form submit

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 class="shorten-btn" type="submit">Shorten It!</button>
  </form>

Server:

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

Upvotes: 1

Views: 77

Answers (1)

Mureinik
Mureinik

Reputation: 311518

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

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

Upvotes: 1

Related Questions