user3432681
user3432681

Reputation: 664

How to send a file with NodeJS and express.router?

I'm trying to send a file from nodeJS and express.router to a client. But i get this error:

...webserver/node_modules/express/lib/response.js:412 if (done) return done(err); ^

TypeError: done is not a function at /home/alex/project/webserver/node_modules/express/lib/response.js:412:22 at SendStream.ondirectory (/home/pthong/project/webserver/node_modules/express/lib/response.js:986:5) at emitNone (events.js:67:13) at SendStream.emit (events.js:166:7) at SendStream.redirect (/home/alex/project/webserver/node_modules/express/node_modules/send/index.js:401:10) at onstat (/home/alex/project/webserver/node_modules/express/node_modules/send/index.js:622:41) at FSReqWrap.oncomplete (fs.js:82:15)

And this is my code:

router.get('/getFile/:filename', function(req,res){
    res.sendFile(__dirname, '../uploads', req.params.filename);
});

And the http request is: http://dummy.com/getFile/audio-461074839300.3gpp

-- and if I try this:

res.sendFile('/uploads/'+ req.params.filename);

I get the next error:

Error: ENOENT: no such file or directory, stat '/uploads/audiomessage-10207974875988003-1327917607235274-1461074839300.3gpp' at Error (native)

this is the structure of the webserver: webs

Upvotes: 0

Views: 2801

Answers (1)

hmSchuller
hmSchuller

Reputation: 116

Try res.sendFile(__dirname + filepath/filename)

In your case, this might be:

res.sendFile(__dirname + "/uploads/" + req.params.filename);

Upvotes: 5

Related Questions