I found other similar questions regarding Multer, but with no answers. I'm trying to upload a file with next.js (front-end) and node.js (back-end). The data is being posted via the network tab when using dev tools.
Below is my setup:
app.js
const express = require('express');
const bodyParser = require('body-parser');
// Get routes
const routeUser = require('./routes/user');
// Create an express server
var app = express();
// Add necessary middleware
app.use(bodyParser.urlencoded({ extended: true })); // Support encoded bodies
app.use(bodyParser.json({
type: ["application/x-www-form-urlencoded", "application/json"], // Support json encoded bodies
}));
// Custom routes
routeUser(app);
// Start server on port 1234
app.listen(1234, () => {
console.log("API is running.");
});
route/user.js
const multer = require('multer');
module.exports = function(app) {
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./user_photos");
},
filename: (req, file, cb) => {
cb(null, file.originalname)
}
})
});
app.post('/user/update', upload.single('user_photo'), (req, res) => {
console.log(req.body, req.file);
});
}
Form
submit(event) {
event.preventDefault();
let form_values = new FormData(event.target);
fetch(this.props.env.api + "/user/update", {
method: 'post',
headers: {
'Content-Type': 'multipart/form-data; boundary=MyBoundary',
},
body: form_values
}).then((response) => {
return response.json();
}).then((response) => {
console.log(response);
});
}
------
<form onSubmit={this.submit}>
<input type="file" name="user_photo"/>
<button type="submit">Upload</button>
</form>
I've tried several tutorials, I'm setting it up according to the docs, yet I keep getting the following error:
Error: Unexpected end of form
at Multipart._final (...\node_modules\busboy\lib\types\multipart.js:588:17)
If I remove multipart/form-data as Content-Type, the form submits with no problem, but with no file. My guess it has something to do with the way the formData object is being received.
Hey #SReca, just faced this issue today and hope I can help you out here and anybody else reading this.
Resolving Unexpected end of form
Regarding the original issue about Unexpected end of form, you are correct in removing the Content-Type as a solution. This is because sending FormData() with regular Fetch or XMLHttpRequest will automatically have the header set by the Browser. The Browser will also attach the boundary needed in all multipart/form-data requests in order indicate when a form starts and ends. More details can be found on MDN's docs about Using FormData Objects... there's a warning about explicitly setting Content-Type.
Potential fix for missing file
Now, about the missing file, it's possible that it has an incorrect reference to the path you're expecting. I am not using diskStorage, but I am using regular dest option to save my file and had the same problem (wanted to save images to ./assets/images). What worked for me was to supply the complete directory path. Maybe changing your callback function in the destination property to
// Assuming the the path module is 'required' or 'imported' beforehand
cb(null, path.resolve(__dirname, '<path-to-desired-folder>'));
will solve the issue. Hope this helps!
What caused this too me was because I was using multiple middleware on the same route, I was using global middleware and then applied another middleware in sub route. so this caused conflits.
In my case the problem magically went away by downgrading Multer to 1.4.3.
See https://github.com/expressjs/multer/issues/1144
Here is my simple form:
<form id="loginformA" action="userlogin" method="post">
<div>
<label for="email">Email: </label>
<input type="text" id="email" name="email"></input>
</div>
<input type="submit" value="Submit"></input>
</form>
Here is my Express.js/Node.js code:
app.post('/userlogin', function(sReq, sRes){
var email = sReq.query.email.;
}
I tried sReq.query.email or sReq.query['email'] or sReq.params['email'], etc. None of them work. They all return undefined.
When I change to a Get call, it works, so .. any idea?
Things have changed once again starting Express 4.16.0, you can now use express.json() and express.urlencoded() just like in Express 3.0.
This was different starting Express 4.0 to 4.15:
$ npm install --save body-parser
and then:
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
The rest is like in Express 3.0:
Firstly you need to add some middleware to parse the post data of the body.
Add one or both of the following lines of code:
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
Then, in your handler, use the req.body object:
// assuming POST: name=foo&color=red <-- URL encoding
//
// or POST: {"name":"foo","color":"red"} <-- JSON encoding
app.post('/test-page', function(req, res) {
var name = req.body.name,
color = req.body.color;
// ...
});
Note that the use of express.bodyParser() is not recommended.
app.use(express.bodyParser());
...is equivalent to:
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());
Security concerns exist with express.multipart(), and so it is better to explicitly add support for the specific encoding type(s) you require. If you do need multipart encoding (to support uploading files for example) then you should read this.
Security concern using express.bodyParser()
While all the other answers currently recommend using the express.bodyParser() middleware, this is actually a wrapper around the express.json(), express.urlencoded(), and express.multipart() middlewares (http://expressjs.com/api.html#bodyParser). The parsing of form request bodies is done by the express.urlencoded() middleware and is all that you need to expose your form data on req.body object.
Due to a security concern with how express.multipart()/connect.multipart() creates temporary files for all uploaded files (and are not garbage collected), it is now recommended not to use the express.bodyParser() wrapper but instead use only the middlewares you need.
Note: connect.bodyParser() will soon be updated to only include urlencoded and json when Connect 3.0 is released (which Express extends).
So in short, instead of ...
app.use(express.bodyParser());
...you should use
app.use(express.urlencoded());
app.use(express.json()); // if needed
and if/when you need to handle multipart forms (file uploads), use a third party library or middleware such as multiparty, busboy, dicer, etc.
Note: this answer is for Express 2. See here for Express 3.
If you're using connect/express, you should use the bodyParser middleware: It's described in the Expressjs guide.
// example using express.js:
var express = require('express')
, app = express.createServer();
app.use(express.bodyParser());
app.post('/', function(req, res){
var email = req.param('email', null); // second parameter is default
});
Here's the original connect-only version:
// example using just connect
var connect = require('connect');
var url = require('url');
var qs = require('qs');
var server = connect(
connect.bodyParser(),
connect.router(function(app) {
app.post('/userlogin', function(req, res) {
// the bodyParser puts the parsed request in req.body.
var parsedUrl = qs.parse(url.parse(req.url).query);
var email = parsedUrl.email || req.body.email;;
});
})
);
Both the querystring and body are parsed using Rails-style parameter handling (qs) rather than the low-level querystring library. In order to parse repeated parameters with qs, the parameter needs to have brackets: name[]=val1&name[]=val2. It also supports nested maps. In addition to parsing HTML form submissions, the bodyParser can parse JSON requests automatically.
Edit: I read up on express.js and modified my answer to be more natural to users of Express.
This will do it if you want to build the posted query without middleware:
app.post("/register/",function(req,res){
var bodyStr = '';
req.on("data",function(chunk){
bodyStr += chunk.toString();
});
req.on("end",function(){
res.send(bodyStr);
});
});
That will send this to the browser
email=emailval&password1=pass1val&password2=pass2val
It's probably better to use middleware though so you don't have to write this over and over in each route.
Note for Express 4 users:
If you try and put app.use(express.bodyParser()); into your app, you'll get the following error when you try to start your Express server:
Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
You'll have to install the package body-parser separately from npm, then use something like the following (example taken from the GitHub page):
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
app.use(function (req, res, next) {
console.log(req.body) // populated!
next();
})
Given some form:
<form action='/somepath' method='post'>
<input type='text' name='name'></input>
</form>
Using express
app.post('/somepath', function(req, res) {
console.log(JSON.stringify(req.body));
console.log('req.body.name', req.body['name']);
});
Output:
{"name":"x","description":"x"}
req.param.name x
Backend:
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json()); // add a middleware (so that express can parse request.body's json)
app.post('/api/courses', (request, response) => {
response.json(request.body);
});
Frontend:
fetch("/api/courses", {
method: 'POST',
body: JSON.stringify({ hi: 'hello' }), // convert Js object to a string
headers: new Headers({ "Content-Type": "application/json" }) // add headers
});
app.use(express.bodyParser());
Then for app.post request you can get post values via req.body.{post request variable}.
Update for Express 4.4.1
Middleware of the following is removed from Express.
bodyParser
json
urlencoded
multipart
When you use the middleware directly like you did in express 3.0. You will get the following error:
Error: Most middleware (like urlencoded) is no longer bundled with Express and
must be installed separately.
In order to utilize those middleware, now you need to do npm for each middleware separately.
Since bodyParser is marked as deprecated, so I recommend the following way using json, urlencode and multipart parser like formidable, connect-multiparty. (Multipart middleware is deprecated as well).
Also remember, just defining urlencode + json, the form data will not be parsed and req.body will be undefined. You need to define a middleware handle the multipart request.
var urlencode = require('urlencode');
var json = require('json-middleware');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.use(json);
app.use(urlencode);
app.use('/url/that/accepts/form-data', multipartMiddleware);
Update
As of Express version 4.16+, their own body-parser implementation is now included in the default Express package so there is no need for you to download another dependency.
You may have added a line to your code that looks like the following:
app.use(bodyparser.json()); //utilizes the body-parser package
If you are using Express 4.16+ you can now replace that line with:
app.use(express.json()); //Used to parse JSON bodies
This should not introduce any breaking changes into your applications since the code in express.json() is based on bodyparser.json().
If you also have the following code in your environment:
app.use(bodyParser.urlencoded({extended: true}));
You can replace that with:
app.use(express.urlencoded()); //Parse URL-encoded bodies
A final note of caution: There are still some very specific cases where body-parser might still be necessary but for the most part Express’ implementation of body-parser is all you will need for the majority of use cases.
(See the docs at expressjs/bodyparser for more details).
I was searching for this exact problem. I was following all the advice above but req.body was still returning an empty object {}. In my case, it was something just as simple as the html being incorrect.
In your form's html, make sure you use the 'name' attribute in your input tags, not just 'id'. Otherwise, nothing is parsed.
<input id='foo' type='text' value='1'/> // req = {}
<input id='foo' type='text' name='foo' value='1' /> // req = {foo:1}
My idiot mistake is your benefit.
For Express 4.1 and above
As most of the answers are using to Express, bodyParser, connect; where multipart is deprecated. There is a secure way to send post multipart objects easily.
Multer can be used as replacement for connect.multipart().
To install the package
$ npm install multer
Load it in your app:
var multer = require('multer');
And then, add it in the middleware stack along with the other form parsing middleware.
app.use(express.json());
app.use(express.urlencoded());
app.use(multer({ dest: './uploads/' }));
connect.json() handles application/json
connect.urlencoded() handles application/x-www-form-urlencoded
multer() handles multipart/form-data
You shoudn't use app.use(express.bodyParser()). BodyParser is a union of json + urlencoded + mulitpart. You shoudn't use this because multipart will be removed in connect 3.0.
To resolve that, you can do this:
app.use(express.json());
app.use(express.urlencoded());
It´s very important know that app.use(app.router) should be used after the json and urlencoded, otherwise it does not work!
Express v4.17.0
app.use(express.urlencoded( {extended: true} ))
app.post('/userlogin', (req, res) => {
console.log(req.body) // object
var email = req.body.email;
}
express.urlencoded
Demo Form
Another Answer Related
Written at Express version 4.16
Inside the router function you can use req.body property to access the post variable. For example if this was the POST route of your form, it would send back what you input:
function(req,res){
res.send(req.body);
//req.body.email would correspond with the HTML <input name="email"/>
}
P.S. for those who are familiar with PHP: In order to access PHP's $_GET variable we use req.query and to access PHP's $_POST variable we use req.body in Node.js.
Request streaming worked for me
req.on('end', function() {
var paramstring = postdata.split("&");
});
var postdata = "";
req.on('data', function(postdataChunk){
postdata += postdataChunk;
});
I could find all parameters by using following code for both POST and GET requests.
var express = require('express');
var app = express();
const util = require('util');
app.post('/', function (req, res) {
console.log("Got a POST request for the homepage");
res.send(util.inspect(req.query,false,null));
})
from official doc version 4
const express = require('express')
const app = express()
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.post('/push/send', (request, response) => {
console.log(request.body)
})
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/',function(req,res){
res.sendfile("index.html");
});
app.post('/login',function(req,res){
var user_name=req.body.user;
var password=req.body.password;
console.log("User name = "+user_name+", password is "+password);
res.end("yes");
});
app.listen(3000,function(){
console.log("Started on PORT 3000");
})
Post Parameters can be retrieved as follows:
app.post('/api/v1/test',Testfunction);
http.createServer(app).listen(port, function(){
console.log("Express server listening on port " + port)
});
function Testfunction(request,response,next) {
console.log(request.param("val1"));
response.send('HI');
}
Use express-fileupload package:
var app = require('express')();
var http = require('http').Server(app);
const fileUpload = require('express-fileupload')
app.use(fileUpload());
app.post('/', function(req, res) {
var email = req.body.email;
res.send('<h1>Email :</h1> '+email);
});
http.listen(3000, function(){
console.log('Running Port:3000');
});
You are using req.query.post with wrong method req.query.post works with method=get, method=post works with body-parser.
Just try this by changing post to get :
<form id="loginformA" action="userlogin" method="get">
<div>
<label for="email">Email: </label>
<input type="text" id="email" name="email"></input>
</div>
<input type="submit" value="Submit"></input>
</form>
And in express code use 'app.get'
Other answers talk about the middleware to use on the server side. My answer attempt to provide developers with a simple playbook to debug the problem themselves.
In this question, the situation is:
You have a form on the client side
The data is sent by clicking on Submit button and you don't use JavaScript to send requests (so no fetch, axios, xhr,... but we can extend the solution for these cases later)
You use Express for the server side
You cannot access data in your Express code. req.body is undefined
There are some actions that you can do to find the solution by yourself:
Open the Network tab and search for your request.
Check the request header Content-Type. In this situation (form submit), the header is likely application/x-www-form-urlencoded or multipart/form-data if you send a file (with enctype="multipart/form-data" in the form tag)
Now check your Express code if you use the appropriate middleware to parse incoming requests.
If your Content-Type is application/x-www-form-urlencoded then you should have app.use(express.urlencoded({extended: true})) in your code.
If your Content-Type is multipart/form-data: because Express doesn't have a built-in middleware to parse this kind of request, you should another library for that. multer is a good one.
If you have done all the steps above, now you can access data in req.body :). If you have problems with the syntax, you should check the Express document page. The syntax could be changed because this question is posted a long time ago.
Now, we can extend this simple playbook for other cases that involve JavaScript code. The key is to check the request Content-Type. If you see application/json then you should use app.use(express.json()) in your code.
In summary, find your request Content-Type, then use the appropriate middleware to parse it.
when you are using POST method in HTML forms, you need to catch the data from req.body in the server side i.e. Node.js.
and also add
var bodyParser = require('body-parser')
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({extended: false}));
OR
use method='GET' in HTML and and catch the data by req.query in the server side i.e. Node.js
I am wanting to post an image in the form of binary to my Express app.
I'm assuming it should come through in the req.body object but will need some form of middleware to be able to handle binary data?
When I send an image as binary from postman and try log req.body, the object is empty.
I am using express-generator as a boilder plate which comes with body-parser like so:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
I had a look at Multer but think that is just for multipart data
Also looked at busboy but couldn't figure out if that will handle binary data.
Am I correct that the post data will still come through in req.body?
And what middleware do I need to handle binary data?
Thanks
The method I ended up using:
const multer = require('multer')
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
router.post('/upload', upload.single('image'), function(req, res, next) {
const image = req.file.buffer
});
Unfortunately, you can't use the body-parser to handle the binary data like files and stuff like that. But wut you can do is use a module call formidable to handle this
Example snipper
app.post('/', (req, res) => {
const form = new formidable.IncomingForm();
form.parse(req, (error, fields, files) => {
if(error){
console.log(error)
}
console.log(fields.name)
const cuteCat = files.cat_image;
console.log(cuteCat.name) // The origin file name
console.log(cuteCat.path) // The temporary file name something like /tmp/<random string>
})
});
<input name="cat_image" type="file" />
<input name="name" type="text" />
I am having troubles getting file uploads to work with NodeJS. I am using Dropzone.JS to create a form that sends a POST request to /file-upload here:
<form action="/file-upload" class="dropzone dragndrop" id="my-awesome-dropzone"></form>
Then I have a route in app.js:
app.post('/file-upload', routes.upload);
Then my handler:
exports.upload = function(req, res){
console.log(req.files);
res.send("OK");
}
However, the upload function here is never called. The server crashes with this error first:
events.js:69
throw arguments[1]; // Unhandled 'error' event
^
Error: Invalid data
at WriteStream._write (fs.js:1616:31)
at onwrite (_stream_writable.js:265:14)
at WritableState.onwrite (_stream_writable.js:94:5)
at fs.js:1628:5
at Object.wrapper [as oncomplete] (fs.js:475:5)
at process._makeCallback (node.js:321:24)
So I am not sure what I should do because it appears that this is not my fault. I followed other tutorials and saw nothing wrong. Also, when I inspect my Network under chrome dev tools, it shows:
Request URL:http://localhost:3000/file-upload
**Request Headers**
Accept:application/json
Cache-Control:no-cache
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryMmLSkbfQskfIcjfE
Origin:http://localhost:3000
Pragma:no-cache
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
X-File-Name:Screenshot from 2013-03-20 12:23:42.png
X-Requested-With:XMLHttpRequest
**Request Payload**
------WebKitFormBoundaryMmLSkbfQskfIcjfE
Content-Disposition: form-data; name="file"; filename="Screenshot from 2013-03-20 12:23:42.png"
Content-Type: image/png
------WebKitFormBoundaryMmLSkbfQskfIcjfE--
#user568109 and #nick-fishman are correct; you should use the bodyParser middleware for this.
Please see the sample code for a basic file upload form below. (Note: you will need to create an "uploads" directory to store the files.)
file-upload.js:
var express = require("express"),
app = express();
// tell express to use the bodyParser middleware
// and set upload directory
app.use(express.bodyParser({ keepExtensions: true, uploadDir: "uploads" }));
app.engine('jade', require('jade').__express);
app.post("/upload", function (request, response) {
// request.files will contain the uploaded file(s),
// keyed by the input name (in this case, "file")
// show the uploaded file name
console.log("file name", request.files.file.name);
console.log("file path", request.files.file.path);
response.end("upload complete");
});
// render file upload form
app.get("/", function (request, response) {
response.render("upload_form.jade");
});
app.listen(3000);
views/upload_form.jade:
doctype 5
html
head
title Upload Form
body
h1 Upload File
form(method="POST", action="/upload", enctype="multipart/form-data")
input(type="file", name="file")
input(type="submit")
#user568109 is correct: you need ExpressJS and bodyParser enabled. Do you have a line similar to the following in your configuration?
app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));
Try use busboy-body-parser to retrieve the request body parameters and the files.
start.js
var bodyParser = require('body-parser');
var busboyBodyParser = require('busboy-body-parser');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}));
// parse application/json
app.use(bodyParser.json());
//parse multipart/form-data
app.use(busboyBodyParser());
controllers/someController.js
someAction: function(req,res){
if(req.method == "POST"){
res.end(JSON.stringify(req.body)+JSON.stringify(req.files));
}
}
//{"text":"testx"}{"anexo":{"data":{"type":"Buffer","data":.... }}}
//req.body = {"text":"testx"}
//req.files = {"anexo":{"data":{"type":"Buffer","data":.... }}}
views/someController/someAction.html
<form method="post" id="multipart" enctype="multipart/form-data">
<input type="text" id="text1" name="text" value="testx" />
<input type="file" id="anexo" name="anexo" />
<input type="submit" value="Enviar" />
</form>
To create a file uploaded, you need work if the stream, for example:
/* file props
{
"data":{"type":"Buffer","data":.... },
"fieldname":"anexo",
"originalname":"images (1).jpg",
"encoding":"7bit",
"mimetype":"image/jpeg",
"destination":"c:\\live\\sources\\uploads\\",
"filename":"eventclock_images(1)_1443706175833.jpg",
"path":"c:\\live\\sources\\uploads\\eventclock_images(1)_1443706175833.jpg",
"size":9986
}
*/
var fileStream = fs.createWriteStream(file.path);
fileStream.write(file.data);
fileStream.end();
fileStream.on('error', function (err) {
//console.log("error",err);
});
fileStream.on('finish', function (res) {
//console.log("finish",res);
});
Most recent up-to-date version:
const express = require("express");
const app = express();
const bodyParser = require('body-parser');
const multer = require('multer');
// serve your HTML form
app.use(express.static('public'));
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse multipart/form-data
app.use(multer({dest:__dirname+'/uploads/'}).any());
app.post("/upload", function (request, response) {
// request.files will contain the uploaded file(s),
// keyed by the input name (in this case, "file")
// show the uploaded files
console.log("file name", request.files);
response.end("upload complete");
});
app.listen(3000, "0.0.0.0");
npm install express --save
npm install body-parser --save
npm install multer --save