1

I'm using the express.json() before the app.patch but both req.body & req.params.id return undefined. I've tried to change the route to /:id but it didn't work. It works for app.get and app.post.

Headers: Content-Type: application/json

const express = require("express");
const dotenv = require("dotenv")
dotenv.config({ path: ".env" });

const app = express();

app.use(express.json());
    
app.patch("/api/v1/", (res, req, next) => {
  console.log(req.body);
});

const PORT = process.env.PORT || 5000;
app.listen(
  PORT,
  console.log(
    `Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold
  )
);

Any ideas? Thank you.

trunks
  • 147
  • 2
  • 9
  • Does this answer your question? [Express.js req.body undefined](https://stackoverflow.com/questions/9177049/express-js-req-body-undefined) – callback Jan 25 '21 at 22:08
  • Is there a reason you NEED to use patch? I understand the urge to use these fancy new HTTP VERBS, but that's just it, they don't necessarily add much besides nice verbage. – Thomas Yamakaitis Jan 25 '21 at 22:10
  • @callback I'm using Express 4 which has build-in body parser. – trunks Jan 25 '21 at 22:17

1 Answers1

1

You have it mixed up.

req comes before res.

app.patch("/api/v1/", (req, res, next) => {
  console.log(req.body);
});

Also, make sure you add a / before api.

Thomas Yamakaitis
  • 417
  • 2
  • 6
  • 20