MrMangado
MrMangado

Reputation: 993

Strange error downloading files with Node.JS

This is the Node.JS code, it has two parts. The first one is in the main server file:

app.get('/download/:photo', function (req, res){ 

   dbs.imagenschema.find({ _id: req.params.photo }, function (err, imagen){

     if (err) throw assert.ifError(err);;

     var file = __dirname + '/public/users/' + imagen.auid + '/' + imagen.name;

     var filename = path.basename(file);
     var mimetype = mime.lookup(file);

     res.setHeader('Content-disposition', 'attachment; filename=' + filename);
     res.setHeader('Content-type', mimetype);

     var filestream = fs.createReadStream(file);
     filestream.pipe(res);

   });

});

The other part is the dbs.imagenschema, but that part is very simple, it's just the schema for the images exported from dbs module.

This is te button that when is clicked, the download starts:

<a href="/download/<%= photo._id %>">
  <button class="btn btn-inverse"><i class="icon-download-alt icon-white"></i>  Download</button>
</a>

But, when I click it, I receive this error:

TypeError: Cannot read property 'background' of null
  at eval (eval at <anonymous> (/root/pixgalery/node_modules/ejs/lib/ejs.js:234:12), <anonymous>:30:4196)
  at /root/pixgalery/node_modules/ejs/lib/ejs.js:239:15
  at Object.exports.render (/root/pixgalery/node_modules/ejs/lib/ejs.js:277:13)
  at View.exports.renderFile [as engine] (/root/pixgalery/node_modules/ejs/lib/ejs.js:303:22)
  at View.render (/root/pixgalery/node_modules/express/lib/view.js:75:8)
  at Function.app.render (/root/pixgalery/node_modules/express/lib/application.js:500:10)
  at ServerResponse.res.render (/root/pixgalery/node_modules/express/lib/response.js:717:7)
  at /root/pixgalery/app.js:181:15
  at Promise.<anonymous> (/root/pixgalery/db.js:237:27)
  at Promise.<anonymous> (/root/pixgalery/node_modules/mongoose/lib/promise.js:133:8)

So, I think is because of this:

app.get('/:user/:id', function (req, res){

  dbs.getimage(req.params.id, req.params.user, function (user, img, fav, comments){

    if (img.type == 'music') {

      res.render('music.ejs', {

            user: user,
            photo: img,
            req: req.session.user,
            fav: fav,
            comments: comments

        });

    } else{

      res.render('photo.ejs', {

            user: user,
            photo: img,
            req: req.session.user,
            fav: fav,
            comments: comments

      });

    }

  });

});

Maybe the server thinks that the /download/fooid is a request for the code I've writted above, because, the /download/fooid doesn't send any response with an EJS file, so, is impossible to get an error like that. Any solution for this?

Thank's advance!

Upvotes: 0

Views: 269

Answers (1)

robertklep
robertklep

Reputation: 203359

You have to make sure the /download/:photo route is declared before the /:user/:id route, otherwise the latter will also match /download/fooid, thinking that download is the user parameter and fooid is the id parameter (with Express, matching occurs in order of declaration: the first route which matches a request will get to handle it).

So:

// first this...
app.get('/download/:photo', ...);

// ...and then this.
app.get('/:user/:id', ...);

Upvotes: 1

Related Questions