Search code examples
node.jsexpressasync-awaitcouchdbcouchdb-nano

nodejs: multiple GET requests with async nano/express not working


I have a superstrange error with node.js and express which is driving me nuts for two days now.

I want to display a series of images on my web app. Therefore, I'm sending a GET request from the client to my express API, which then should deliver the image.

It works perfectly with only ONE image per page.

However, if I want to display a series of images, let's say 8 images, ONLY THE LAST IMAGE IS BEING RENDERED! But the order changes occassionaly, sometimes it's the penultimate image that works, it's being shuffled in a complete random order!

But it's not only a problem with images - it's the same behaviour with ALL (async) requests! For example, if I want to render some usernames to an iframe, I only get the data for the last iframe, all others show mit a 404 error with CANNOT GET.

This is my code on the frontend:

<iframe src="http://127.0.0.1:3000/files/bigThumb/file-version-2017-12-27T11-53-45-647Z-3DnsDX?projectdb=cdu_regierung&companydb=cdu&authsession=supersecrettoken"></iframe>
<iframe src="http://127.0.0.1:3000/files/bigThumb/file-version-2017-12-27T13-08-58-189Z-q52KKd?projectdb=cdu_regierung&companydb=cdu&authsession=supersecrettoken"></iframe> 
<iframe src="http://127.0.0.1:3000/files/bigThumb/file-version-2017-12-27T13-08-58-189Z-q52KKd?projectdb=cdu_regierung&companydb=cdu&authsession=supersecrettoken"></iframe> 
<iframe src="http://127.0.0.1:3000/files/bigThumb/file-version-2017-12-27T13-08-58-189Z-q52KKd?projectdb=cdu_regierung&companydb=cdu&authsession=supersecrettoken"></iframe> 

   

this is my code in on the server side

app.all('/files/:action/:versionId',  async function(req, res) {

try {
	
    var projectName = req.query.projectdb;
    var companyName = req.query.companydb;
    var authSession =  req.query.authsession;
		
    var nano = _nano({url: 'http://127.0.0.1:5984/',  cors: true, cookie: 'AuthSession='+ authSession});
		
    var session = await nano.session();
    session = session[0];
    var username = session.userCtx.name;
		
    res.send(username);
	
  } catch(err) {
    return res.status(401).send(err);
  }

})

My guess is that it has something to do with ASYNC function in

app.all('/files/:action/:versionId',  async function(req, res) {

as I never had this problem with standard sync function(req, res)

What am I doing wrong??

EDIT I have them same problem with this code below.

app.all('/files/:action/:versionId', function(req, res) {
    request('https://jsonplaceholder.typicode.com/posts/1', function (error, response, body) {
        res.send(body);
});

It works perfectly with 1 GET, but not with 8 simultaneous GET requests. Also, I'm getting this error in the log:

_http_outgoing.js:494
throw new Error('Can\'t set headers after they are sent.');

Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)

Solution

  • I found the solution - it's a bug caused by the nodejs middleware "express-formidable". The issue is discussed here. https://github.com/utatti/express-formidable/issues/6

    Just use the "formidable" middleware and you're good to go. This is the code I ended up with.

    var formidable = require('formidable');
    
    // init formidable middleware
    app.use(function (req, res, next) {
    	var form = new formidable.IncomingForm({
    		encoding: 'utf-8',
    		multiples: false,
    		keepExtensions: true,
    	})
    	form.once('error', console.log)
    	form.parse(req, function (err, fields, files) {
    		Object.assign(req, {fields, files});
    		next();
    	})
    });