Is there a specific way I should set-up my body-parser to deal with large volumes of Text like articles when I am making a post to Postman? I am getting an entity-parse error when I try to make a post on Postman and the terminal in my IDE flags up the below segments of code that are causing the error. Do I need to set the parser to allow higher volumes of text?
My routes, controller, model etc match to Postgres so I believe the error is somewhere in my server.js file to do with BodyParser.
user.routes.js segment that is being flagged in IDE terminal
**app.use(function (req, res, next) {
res.header(
"Access-Control-Allow-headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});**
error message on console
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header
...
...backend\server\routes\user.routes.js:7:13
...
...backend\server.js:67:5
server.js
**app.use((err, req, res, next) => {
res.status(500).json({
error: err,
message: 'Internal server error!',
})
next()
})**
json posting on Postman
{
"articleTitle": "Amazon’s Plastic Problem Revealed",
"articleContent": "Oceana released a report based on an analysis of e-commerce packaging data that found Amazon generated 465 million pounds of plastic ...... want and offer plastic-free packaging as an option at checkout.”,
"articlePhotos": null,
"articleAuthor": 6,
"articleType": 2
}
error message on postman
{
"error": {
"expose": true,
"statusCode": 400,
"status": 400,
"body": "{\r\n \"articleTitle\": \"Amazon’s Plastic Problem Revealed\",\r\n \"articleContent\": \"Oceana released a report based on an analysis of e-commerce packaging data that found Amazon generated 465 million pounds of plastic packaging waste last year. This is comprised of the .... Listen to its customers: As an immediate measure Amazon should give its customers what they want and offer plastic-free packaging as an option at checkout.”,\r\n \"articlePhotos\": null,\r\n \"articleAuthor\": 6,\r\n \"articleType\": 2\r\n}\r\n",
"type": "entity.parse.failed"
},
"message": "Internal server error!"
}
server.js
const express = require("express");
const bodyParser = require ("body-parser");
// creates an en express app
const app = express();
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-headers',
'Origin, X-Requested-With, Content-Type, Accept',
);
next();
});
// parse requests of content-type
app.use(bodyParser.json({limit: '50mb'}));
//bp used to process form data as json
app.use(bodyParser.urlencoded({limit: '50mb', extended: true }));
...
try {
db.sequelize.authenticate();
console.log('Connection made to the database made.');
} catch (error) {
console.error('unable to connect:', error);
}
// db.sequelize.sync();
// force: true will drop the table if it already exists
db.sequelize.sync({ force: false })
.then(() => {
console.log(`Drop and re-sync database with { force: true }`)
//initial()
});
// simple route
app.get('/', (req, res) => {
res.json({ message: "Connected to the sustainable scuba database." });
});
app.disable('x-powered-by')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use((err, req, res, next) => {
res.status(500).json({
error: err,
message: 'Internal server error!',
})
next()
})
app.listen(9001, () => {
console.log('app now listening for requests!!!')
})