Find a File Based on Route

29 views
Skip to first unread message

Grant MacDonald

unread,
Jun 7, 2015, 9:08:32 PM6/7/15
to nod...@googlegroups.com
Hi folks.  I have a requirement that when the user enters the route and a file name (ie. http://website/filename.pdf) that my node.js server will determine which path (out of many) where the file exists (ie. rather than generating a 404.)  The location of the file will be determined by the name of the file passed in the URL.  I'm not sure the best way to do this (I am using Express 4.)  Should I do the file location check when I catch the 404?

Thanks. Grant.

Grant MacDonald

unread,
Jun 8, 2015, 8:53:03 AM6/8/15
to nod...@googlegroups.com
Found a way to do what I want...do a get on router.get('*',...then parse req.url.

Hage Yaapa

unread,
Jun 8, 2015, 8:53:22 AM6/8/15
to nod...@googlegroups.com
Use the built-in middleware express.static -
http://expressjs.com/starter/static-files.html

Trying to implement this feature on your own can be risky if not done properly.

On Mon, 8 Jun 2015 at 6:38 am Grant MacDonald <minevill...@gmail.com> wrote:
Hi folks.  I have a requirement that when the user enters the route and a file name (ie. http://website/filename.pdf) that my node.js server will determine which path (out of many) where the file exists (ie. rather than generating a 404.)  The location of the file will be determined by the name of the file passed in the URL.  I'm not sure the best way to do this (I am using Express 4.)  Should I do the file location check when I catch the 404?

Thanks. Grant.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/08e09e49-c71d-402c-ad3e-bf52843607cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages