I need to write a handler for all types of form. I check the work via postman. What I've just written works for all forms except binary.
As a result, I get the error
PayloadTooLargeError: request entity too large
at readStream (/home/iskrayuriicr/projects/implement-echo-server/node_modules/raw-body/index.js:155:17)
at getRawBody (/home/iskrayuriicr/projects/implement-echo-server/node_modules/raw-body/index.js:108:12)
at read (/home/iskrayuriicr/projects/implement-echo-server/node_modules/body-parser/lib/read.js:77:3)
at rawParser (/home/iskrayuriicr/projects/implement-echo-server/node_modules/body-parser/lib/types/raw.js:81:5)
at Layer.handle [as handle_request] (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/layer.js:95:5)
at next (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/route.js:137:13)
at next (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/route.js:131:14)
at next (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/route.js:131:14)
at next (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/route.js:131:14)
at next (/home/iskrayuriicr/projects/implement-echo-server/node_modules/express/lib/router/route.js:131:14)
My code:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' })
const { raw } = express;
const port = 3000;
app.use(bodyParser.json({limit: '50mb', extended: true}));
app.use(bodyParser.text({limit: '50mb', extended: true}));
app.use(bodyParser.raw({limit: '50mb', extended: true}));
app.use(bodyParser.urlencoded({parameterLimit: 100000, limit: '50mb', extended: true}));
app.all('*',raw({type: '*/*'}), upload.array('files', 12), (req, res) => {
console.log(req);
res.json({
method: req.method,
path: req.originalUrl,
query: req.query,
headers: JSON.stringify(req.headers),
body: {
fields: req.body,
files: req.files
},
});
});
app.listen(port,(err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
how to properly get body from such binary form?
Related
I'm trying to send a json to my nodeJs app through POST Method in body.
For that I'm using POSTMAN to create the request, with the proper consnt-type header and body JSON Rows. Tho the message back is "OK" in console the req.body is {} empty.
Would you have an idea what's wrong in my code?
const bodyParser = require('body-parser');
const { Client } = require('pg');
const express = require('express');
const app = express();
// create application/json parser
const jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
const urlencodedParser = bodyParser.urlencoded({ extended: false })
const hostname = '127.0.0.1';
const port = 3000;
const dbSchema = 'public';
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'postgres',
password: '123123',
port: 5432,
});
client.connect();
/* =========== Some Initialize staff =============== */
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
app.use(bodyParser.urlencoded({
extended: true
}));
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
/* =========== FROM HERE =============== */
app.post('/post-test', urlencodedParser, (req, res) => {
console.log('Got body:', req.body);
res.sendStatus(200);
});
app.get('/',(req,res)=>{
res.status(200).send('Get Ready for something awesome!\n');
});
enter image description here
You should use app.use(bodyParser.json());, in your code const jsonParser = bodyParser.json() this is not used.
Update: Or you can apply jsonParser middleware directly to the post route:
app.post("/post-test", jsonParser, (req, res) => {
console.log("Got body:", req.body);
res.json({ ...req.body });
});
Can't figure what is happening so, I am posting the code that works for me -
let express = require('express');
let app = express();
const authorRoute = express.Router();
authorRoute.use(express.json());
authorRoute.use(express.urlencoded({extended:true}));
authorRoute.post('/post-test', async (req, res) => {//req.body});
app.use(authorRoute);
Also, make sure to test with a well-formed JSON.
version "express": "^4.17.1",
I am trying to send 6 based64 images with some other data but I keep getting error entity is too large even I have added below code in my app.js file
`app.use(bodyparser.json({ limit: '50mb', extended: true }))
app.use(bodyparser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))`
If I try with the postman then there is no error only with web application.
I am using Angular as Front-end.
I don't know why this error is occurring.
Kindly your help would be great.
Thanks in Advance.
const express = require('express');
const bodyParser = require('body-parser')
const multer = require('multer')
var fs = require('fs');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './public');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now() + '.png');
}
});
var upload = multer({ storage: storage });
const app = express();
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
app.post('/', upload.single('file'), (req, res) => {
console.log(req.body.key1);
fs.readFile(req.file.path, function (err, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
});
})
app.listen(3000);
I am trying to build a full stack(MERN) application. When I'm sending a request to my Express server with a JSON, I always see my req.body is empty. I can nowhere see my JSON inside my request.
I have tried using only app.use(bodyParser.json()) and app.use(bodyParser.urlencoded({ extended: true }));,but no change.
index.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // for encoded bodies
console.log("App");
require("./schema/data");
require("./routes/home_routes")(app);
require("./routes/table_routes")(app);
require("./routes/utility_routes")(app);
utility_routes.js
const mongoose = require("mongoose");
const Data = mongoose.model("data");
module.exports = app => {
app.get("/api/search_member", (req, res) => {
console.log("Utility", req);
console.log("Req Done");
// const search = await Data.findById({ member_id: req.body.member_id });
// console.log(search);
});
};
request.body
[0] body: {},
[0] route:
[0] Route {
[0] path: '/api/search_member',
[0] stack: [ [Layer] ],
[0] methods: { get: true } } }
request from client
onSearch = async value => {
console.log(value);
if (value) {
const searchData = await axios.get("/api/search_member", { value });
console.log(searchData);
}
};
You are making a GET request. GET request do not send body. To get req.body, make POST request to server.
Firstly:-
const searchData = await axios.post("/api/search_member", { value });
And Secondly,
app.POST("/api/search_member", async (req, res) => {
console.log("Utility", req);
console.log("Req Done");
});
This is what I use to accept the json
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.json());
app.use(cookieParser());
And in the request set headers like this.
Content-Type : application/json
Both the axios and http libraries from angular works with this settings for me.
I use Axios to sending request to my NodeJS server. This is my request:
let url = 'http://example.com:1337/api/'
let config = {
headers: {
'Content-Type': 'application/json'
}
}
settings = (data) => {
return axios.post(url + 'settings', JSON.stringify(data), config)
.then( res => res.data)
.catch(e => console.log(e))
}
In NodeJS/ExpressJS server:
const express = require('express')
const App = express()
let bodyParser = require('body-parser')
// ... Mongoose and etc.
var cors = require('cors')
App.use(cors())
App.options('*', cors());
App.use("/data", express.static(__dirname + '/data'));
App.use(bodyParser.urlencoded({
extended: true
}));
App.use(bodyParser.json())
App.use('/api', require('./routes/Users'))
App.listen(1337)
But request to http://example.com:1337/api/settings returns (firefox):
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://example.com:1337/api/settings. (Reason: CORS request did not succeed)
Thank you for your solutions.
// server.js
const express = require('express');
const app = express();
const routes = require('./routes');
const PORT = process.env.PORT || 5000;
// configure body parser for AJAX requests
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(routes);
//Server
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}.`);
});
Try this:
const express = require('express')
const App = express()
let bodyParser = require('body-parser')
// ... Mongoose and etc.
App.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
res.header("Access-Control-Allow-Methods", "GET, POST","PUT");
next();
});
App.use("/data", express.static(__dirname + '/data'));
App.use(bodyParser.urlencoded({
extended: true
}));
App.use(bodyParser.json())
App.use('/api', require('./routes/Users'))
App.listen(1337)
I'm currently implementing the body parser in a way that is mentioned below
var rawBodySaver = function (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
}
app.use(bodyParser.urlencoded({ extended: true}))
app.use(bodyParser.json({ verify: rawBodySaver }));
app.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));
app.use(bodyParser.raw({ verify: rawBodySaver, type: function () { return true } }));
Previously, it was just app.use(bodyParser.urlencoded({ extended: true})) and I got my data in req.body.
But due to some new requirements I had to use rawBody. Is there any way where I can get data in their respective formats in both rawBody and body
Right now, only either of rawBody or body works. Both don not work together.
While a tad hacky, I liked your idea of using the verify function to store the raw body somewhere. I believe your problem was caused by calling bodyParser.* too many times. It appears that only the last call does anything.
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
function rawBodySaver (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8')
}
}
app.use(bodyParser.urlencoded({
extended: true,
verify: rawBodySaver
}))
app.post('/', function (req, res, next) {
console.log(`rawBody: ${req.rawBody}`)
console.log(`parsed Body: ${JSON.stringify(req.body)}`)
res.sendStatus(200)
})
// This is just to test the above code.
const request = require('supertest')
request(app)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, () => {})
This prints:
rawBody: user=tobi
parsed Body: {"user":"tobi"}