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);
Related
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?
I'm trynna get the form`s data that contains a file, and I use Multer for handling that, but for some reason, my req.body is returning {}, but when I use the normal form without multipart data, it gives me all the data, so the problem is with Multer, here's my code
const express = require("express");
const app = express();
const multer = require("multer");
app.listen(8000);
let upload = multer({ dest: "uploads/" });
upload.single("logo");
app.use(express.urlencoded({ extended: true }));
app.post("/", (req, res) => {
console.log(req.file);
});
const express = require("express");
const app = express();
const multer = require("multer");
app.listen(8000);
let upload = multer({ dest: "uploads/" });
app.use(express.urlencoded({ extended: true }));
app.post("/", upload.single("logo"), (req, res) => {
console.log(req.file);
});
You need to specify upload.single as middleware.
This is working code in my project.
const path = require("path");
const multer = require('multer');
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'images');
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString()+ "-" + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/jpeg'
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(multer({ storage: fileStorage, fileFilter: fileFilter }).single('image'));
app.use(express.static(path.join(__dirname, 'public')));
app.use("/images", express.static(path.join(__dirname, 'images')));
app.post("/", (req, res) => {
console.log(req.file);
});
app.listen(5000, () => console.log("Server Started!"));
Here is request payload I can see in console
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="file"; filename="Screenshot from 2016-12-12 18-34-26.png"
Content-Type: image/png
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="userId"
12
------WebKitFormBoundaryW5j4rbM04jMUgOkU
Content-Disposition: form-data; name="isAdmin"
true
------WebKitFormBoundaryW5j4rbM04jMUgOkU--
I need to read userId which is there
name="userId"
12
but my req.body is blank JSON {}. how to read this?
app.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
routes.js
var employeeDocumentStorage = multer.diskStorage({
destination: function (req, file, cb) {
****//need to read data here****
console.log("uploading");
console.log(file);
var x = (JSON.parse(req.body));
console.log(x)
//var code = JSON.parse(req.body.model).empCode;
var dest = 'public/documents/vss811/';
mkdirp(dest, function (err) {
if (err) cb(err, dest);
else cb(null, dest);
});
},
filename: function (req, file, cb) {
cb(null, Date.now()+'-'+file.originalname);
}
});
As Alex has said in comments, you won't get this with body-parser. Your request payload shows that you seem to be using form-data content type.
You can use multer to get form-data payloads - and your route already shows you're using multer (var employeeDocumentStorage = multer.diskStorage({)):
const express = require('express')
const upload = require('multer')({ dest: '/tmp/' });
app.post('/some-route', upload.single('name'), function (req, res, next) {
console.log(req.body.userId); // should show 12
console.log(req.files); // that's your file there.
// do stuff.
});
Try this worked for me
const express = require('express');
const bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', upload.none(), function (req, res) {
// req.body would contain userId
console.log("Data",req.body)
res.status(200).send('OK');
});
app.listen(port);
I'm trying ti deploy a NodeJS App to Azure. Everything works fine including react. but when I'm trying to hit the GraphQL server I'm getting a 404 Error
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Any Ideas?
Here is my index.js
'use strict'
require('babel-core/register')
import schema from './data/schema'
import GraphQLHTTP from 'express-graphql'
import express from 'express'
const multer = require('multer')
const insert = require('./business/insert')
const app = express()
const port = process.env.PORT || 8080
var bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}))
app.use(express.static('public'))
app.set('view engine', 'html')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/upload/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.xlsx')
}
})
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.render('index')
})
app.listen(port, () => {
console.log('Listening http://localhost:8080')
})
Judging by this document https://babeljs.io/docs/usage/require/ it appears that tooling for ES6 only happens via the next requires
All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel.
So you'll need something like this instead:
// app.js
'use strict'
require('babel-core/register')
require('./index')
// index.js
import schema from './data/schema'
import GraphQLHTTP from 'express-graphql'
import express from 'express'
const multer = require('multer')
const insert = require('./business/insert')
const app = express()
const port = process.env.PORT || 8080
var bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.use('/graphql', GraphQLHTTP({
schema,
graphiql: true
}))
app.use(express.static('public'))
app.set('view engine', 'html')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/upload/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.xlsx')
}
})
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.render('index')
})
app.listen(port, () => {
console.log('Listening http://localhost:8080')
})
when i run this app and submit the form 'console.log(request.body)' returning undefined. i am a beginner in node js . can anyone please explain what i am doing wrong?
this is the registration form
register.jade
doctype html
html
head
title.
Fonebook
link(rel='stylesheet', href='/assets/bootstrap/css/bootstrap.min.css')
script(src='https://code.jquery.com/jquery-2.2.0.min.js')
script(src='/assets/bootstrap/js/bootstrap.min.js')
body
div.container
h3.
The Fonebook
form(method="POST",class="center",id="regForm",action='/doReg',enctype="multipart/form-data")
div.panel.panel-info
div.panel-heading.
Registration
div.panel-body
div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
label.
Profile Picture:
input(type='file',name='proPic')
div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
label.
Email:
input.form-control(id="email", type="email", name="email", placeholder="Enter email ")
div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
label.
Name:
input.form-control(id="name", type="text", name="name", placeholder="Enter name ")
div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
label.
Password:
input.form-control(id="password", type="password", name="password")
div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
a(href='login').
Already have an account?
div.panel-footer
button.btn.btn-info(type="submit").
Register
here is my app.js file
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/test');
var uploadDir = __dirname + '/uploads';
var fs = require('fs');
var multer = require('multer');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
var conn = mongoose.connection;
app.set('views', './views');
app.set('view engine', 'jade');
app.use('/assets', express.static(__dirname + '/public'));
var port = process.env.PORT || 3000;
app.all('/', function (req, res) {
res.render('login');
})
app.all('/register', function (req, res) {
res.render('register');
})
app.all('/login', function (req, res) {
res.render('login');
})
var storage = multer.diskStorage({
destination: function (request, file, callback) {
callback(null, uploadDir);
},
filename: function (request, file, callback) {
//console.log(file);
callback(null, Date.now() + '.jpg');
}
});
var upload = multer({storage: storage}).single('proPic');
app.all('/doReg', function (request, response) {
upload(request, response, function (err) {
if (err) {
console.log(err);
return;
}
//console.log(request.file);
response.end('Your File Uploaded');
})
console.log(request.body);
});
app.listen(port);
body-parser does not support multipart forms.
From their README
This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:
busboy and connect-busboy
multiparty and connect-multiparty
formidable
multer
Seeing as you're already using multer you can access your fields this way:
app.all('/doReg', upload, function (request, response) {
console.log(request.body);
});