Besides using var bodyParser = require('body-parser');
in server side, it will still return empty req.body that will lead to errors because you have validation in place. The reason it is returning empty req.body when you sending PATCH request using form-data in Postman is because body-parser can't handle multipart/form-data. You need a package that can handle multipart/form-data like multer.
at the first to install the body-parser and multer, go to your terminal and use ?
npm install --save body-parser multer
so add this code to in server.js
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
var app = express();
at the next time use this middelware:
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true }));
//form-urlencoded
// for parsing multipart/form-data
app.use(upload.array());
app.use(express.static('public'));
After importing the body parser and multer, we will use the body-parser for parsing json and x-www-form-urlencoded header requests, while we will use multer for parsing multipart/form-data.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…