Skip to content Skip to sidebar Skip to footer

Express Body Parser With Both Json And Binary Data Passing Capability

In my express app router, I've routes for accepting POST request of JSON data as well as binary data. The problem is when I use body parser for passing JSON data, it also considers

Solution 1:

By specifying

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

your entire app uses the body parser middleware. You could create another middleware to handle whether or not the body parser is used. For example:

const bodyParse = bodyParser.json();
app.use((req, res, next) => {
    if(req.originalUrl == "restOfUrl/file") next();
    elsebodyParse(req, res, next);
});

Post a Comment for "Express Body Parser With Both Json And Binary Data Passing Capability"