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.
express.json()
app.patch
req.body & req.params.id
/:id
app.get
app.post
Headers: Content-Type: application/json
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.
You have it mixed up.
req comes before res.
req
res
app.patch("/api/v1/", (req, res, next) => { console.log(req.body); });
Also, make sure you add a / before api.
/
api
1.4m articles
1.4m replys
5 comments
57.0k users