I’m using express and I’d like to clean up my hard-coded URLs. There seem to be a number of projects which produce urlFor functionality on the server-side. Is there a best practice for doing this on the client-side?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I ended up building a shared module which exports two functions:
pathRaw, andpathFor.pathRawoutputs a path which can be consumed by the express routing mechanism. For example:returns
'/users/:userid/videos/new'. Note that I’m using the pluralization functionality from mongoose.pathFortakes the output frompathRawand replaces the ids. For example:returns
'/users/u1/messages'. Note that additional values are converted to query parameters.There are a few other features like path overrides that I’ve added. I’ll probably put this up on github once the code is a little more battle tested.