0

I have a .gz file located at /public/files/update.tar.gz. I would like to serve it for downloading using the route /ud/files/update.tar.gz. I would like to do it as static content and also with route (as some files will have a dynamic generated route).

I've tried the following for static (Actually achieved with nginx wich maybe is even better):

main.use('/ud/files', express.static(path.join(__dirname, 'public/files')));

Here I get the error:

Error: No default engine was specified and no extension was provided. at new View (bla bla)

And for dynamic url at /ud/files/tX2r8z/update.tar.gz:

router.get('/ud/files/:s/:file', function(req, res) {
  checkSg(req.params.s, function(err) {
    if(!err) {
      res.download("/public/files/" + req.params.file, req.params.file); 'also tried sendFile()
    } else {
      res.send(404);
    }
   });
});

Here I get two errors toghether:

Error: Forbidden at SendStream.error  (bla bla)
Error: No default engine was specified and no extension was provided. at new View (bla bla)

Any ideas?

Miquel
  • 8,339
  • 11
  • 59
  • 82

1 Answers1

0

Ok, found the solution: the first one it was a configuration problem as nginx was rewriting the path in order to serve the file itself (Right now I'm wondering why it wasn't served by nginx and the request was sent to node...):

    location ~ ^/(ud\/files)/ {
        rewrite ^/ud\/files(/.*)$ $1 last;
        root /mnt/data/public/files;
        gzip_static on;
        add_header Cache-Control public;
    }

    location / {
        proxy_pass   http://127.0.0.1:3000;
    }

So node was seeing a GET /updates.tar.gz

The second one has been solved as stated in this thread:

res.download(path.resolve("public/files/" + req.params.file));
Community
  • 1
  • 1
Miquel
  • 8,339
  • 11
  • 59
  • 82
  • Why would you use nginx and nodejs ? Both ( nodejs may be better in some situations ) accomplish the same thing. – Eduard Dec 29 '15 at 09:06
  • I found nginx do things simpler: balancing, serving apache, static and nodejs apps, html caching, logging... so you don't have to implement all those things in node, just config nginx :) – Miquel Dec 30 '15 at 09:22