Thursday, 14 February 2019

How to receive multiple requests

I'm writing an Angular 6 + Express.JS app and now I stuck with the following problem: when there are some multiple requests made at the same time, sometimes (especially when there are more than 4 requests) all of them response with 404 or even get cancelled. Is there any problem with the way I handle requests in Express or I should add some tweaks for concurrent requests?Requests:``` let requests = []; files.forEach((file) => { if (file.type.toLowerCase().includes('zip')) { requests.push(this.unpredictedImagesService.uploadArchive(file).pipe(first())); } else { requests.push(this.unpredictedImagesService.saveImage(file).pipe(first())); } });forkJoin(requests).subscribe( (res) => res.forEach(response => { this.onSave.emit(response); }), (error) => { console.error(error); }, () => { this.close.emit(); } ); ```Express handling routes:``` router.post('/images', formidable({ encoding: 'utf-8', uploadDir: path.resolve(__dirname, '..', '..', 'uploads'), multiples: true, keepExtensions: true }), (req, res, next) => { const image = req.fields; const data = req.files; image.path = data.image.path; const file = fs.createReadStream(image.path); createUnpredictedImage(image).then( result => { if (result) { res.status(200).send(result); } else { console.error("Cannot save unpredicted image"); res.status(400).send("Cannot save unpredicted image"); } }).catch(e => console.error(e.stack)); }); ```Responses:https://i.imgur.com/RQVU7zH.png

Submitted February 14, 2019 at 10:59AM by nikitalpopov

No comments:

Post a Comment