简体   繁体   中英

How to make Node.js wait for response from a large request

I am posting a large number of files that can take potentially minutes to upload. I using a multi-part form to post the files and then waiting for a response from the POST, but this can take several minutes.

How do I make Node/Express wait for this response? As of now, it seems like the request is "timing out" and Node or the browser is re-POSTing the files because it is taking so long. I can see this because my middleware functions are being called multiple times for requests that take too long.

Is there a library to make Node not timeout? Should I be attempting to POST these files in a different fashion? Thanks

var mid = function(req,res,next) {
  console.log('Called');
  next();
};

app.post('/api/GROBID', mid, authenticate, PDFupload.return_GROBID, PDFupload.post_doc, function(req, res) {
  if (res.locals.body == 400) res.send(400);
  /*if (process.env.TEST_ENV == 'unit-testing') {
    res.send(res.locals.body);
  }*/
  res.render('uploads', {files : res.locals.body});
});

Edit: This mid middleware (used as an example) is being called twice. This means the route is being posted to twice. How do I make sure this does not happen?

Is there a library to make Node not timeout?

Express sits on top of Node.js' built-in HTTP server . By default, the timeout is 2 minutes. You can modify its default timeout as below:

var express = require('express');
var app = express();

var port = process.env.PORT || 3000;

app.get('/', function(req, res) {
    res.send('<html><head></head><body><h1>Hello world!</h1></body></html>');
});

var server = app.listen(port);
server.timeout = 1000 * 60 * 10; // 10 minutes

Should I be attempting to POST these files in a different fashion?

Yes, you can use Multer , a node.js middleware for handling multipart/form-data, which is primarily used for uploading files.

And with Multer, you don't have to worry about the timeout anymore. Event the upload time is longer than the timeout, say 2 minutes by default, Express just won't timeout.

Here is the sample code:

app.js

var express = require('express');
var app = express();
var path = require('path');
var multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/your/path/to/store/uploaded/files/')
  },
  filename: function (req, file, cb) {
    // Keep original file names
    cb(null, file.originalname)
  }
})
var upload = multer({ storage: storage })

// files is the name of the input html element
// 12 is the maximum number of files to upload
app.post('/upload', upload.array('files', 12), async (req, res) => {
  res.send('File uploaded!');
})

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(3000);

index.html

<html>

<body>
  <form ref='uploadForm' id='uploadForm' 
    action='http://localhost:3000/upload' 
    method='post' 
    encType="multipart/form-data">

    <input type='file' name='files' multiple/>

    <input type='submit' value='Upload!' />
  </form>
</body>

</html>

Now try starting the web server:

node app.js

Then open your browser and go to http://localhost:3000

You can now upload a number of big files, which you can find later on in folder /your/path/to/store/uploaded/files/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM