How to handle expressjs middleware request. POST request remains pending made by dropzone

2.2k views Asked by At

I am using dropzone to upload the file. On the server side I am using node and expressjs. I am also using multer as the file handling middleware.

dropzone makes a call to one endpoint to save the file on the server.

On the server side, I created an endpoint to handle the file:

exports.saveFile = function (req, res) {
  console.log(req.files);
  return res.status(200);
  //I have not added any file save logic here
};

This is how I have configured multer

var app = express();
var server = require('http').createServer(app);
app.use(multer({ dest: './uploads/'}));

Looks like multer automatically saves the file into the uploads folder.

Problem: Even though the file is saved on the server, the request remains in pending state on the client side.

How can I return success response from the server to the client?

Do I need to add some code on the endpoint I have created or I need to add required logic under onFileUploadComplete() given by multer?

enter image description here

1

There are 1 answers

2
jfriend00 On BEST ANSWER

You need an Express route for your form post that will finish the http request. The multer middleware will process the file uploads and add them to the request so they are available to a route handler, but you need to have a route for the form post.

From the multer example on this page, you can do something like this for your route:

app.post('/myform', function(req, res){
    console.log(req.body);  // form fields
    console.log(req.files); // form files
    res.status(204).end()
});