<a hrerf=""> for download files with express and Node.JS

With Apache this this really easy, but with Node.JS I don't think so. Simply, I want that when a user click something like this: <a href="/dir/file.png">, he starts to download the file. When I do that, I'm redirected to http://foo.com/fir/file.png, and I visualize the photo. Also say that I have in the app configure this app.use(express.static(__dirname + '/public'));, so, the file is in the public path, and the public path is declared as static. And, when the users click, I dont want that he will be redirected and then the download starts.

I know this is posible, it's just click a link and a download starts! But I don't know how to do it.

Thank's advance!

EDITED:

The HTML where is the <a href="">for the download is here:

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

  usermodel.findOne({ user: req.params.user }, function (err, user){

   var imagen = user.imagen.id(req.params.id);

    if (err) throw err;

        res.render('photo.ejs', {
            user: user,
            photo: imagen,
        });
    });
});

Is necessary to create a new app.get? If I do that, I would get redirected? I'm trying to do this without getting redirected.

This depends on the headers you're sending with the image.

To make the browser download the image instead of displaying it, you need to set a "Content-Disposition: attachment" header.

Since you're using the static middleware, this is a bit trickier to do than in your own request handler function. You'll have to inject a middleware before the static one.

app.use(function(req, res, next) {
  if (req.path.split('/')[0] === "downloads")
    res.attachment(); //short for res.set('Content-Disposition', 'attachment')
  next();
});

app.use(express.static(__dirname + '/public'));

Similarly to @rdrey's answer, make the browser download a file if there's ?dl query parameter in the url.

app.use('/gifs/*.gif', function(req, res, next) {
  if (req.query.dl !== undefined) res.attachment();
  next();
});

app.use('/gifs', express.static(__dirname + '/gifs'));