Home » Nodejs » Use NodeJS to upload file in an API call

Use NodeJS to upload file in an API call

Posted by: admin November 30, 2017 Leave a comment


I’m looking at using NodeJS to act as the server to build an API.

Ideally I’d love for there to be an API endpoint to send a set of information as well as a file which can be saved to the files system.

Most examples I’ve seen are for sending a file via a form, however I’d like to do this through a post request.

Does anyone know how I could achieve this (if it’s at all possible)?

At the moment what I’d like to achieve is something along the following lines:

app.post('/Some/Endpoint/', controller.handleSomeEndpoint, function(request, response) {
    response.send('Finished Request');

exports.handleSomeEndpoint = function(request, response, next) {
    var bodyarr = []
    request.on('data', function(chunk){
    request.on('end', function(){
      console.log( bodyarr.join('') );

But the data and end never get called if I run a curl command along the lines of:

curl -F '[email protected]_file'



The answer seems to be that expressJS doesn’t use the same method of handling a post file as the http module in nodejs.

All that was needed was including a directory for the files to be written to:


Which I found here:



I would suggest you to use Formidable to avoid anonymous file uploading.


Your code should work fine; it’s the curl usage that’s wrong. Try this instead:

$ curl -X POST --data-binary @test_file http://localhost:8080