jxia-ux
jxia-ux

Reputation: 87

best practice for setting dynamic route parameter in express

For my app, I want the routes to be structured so that going to www.myapp.com/(username) will take you to that user's page. The way I set this up is by using the following handler.

app.get('/:username', function(){
  //look up user profile data from db and render the page
});

This setup works but is it the proper way to do it? I have to put this handler below my other ones because otherwise :username will try to render anything thats passed to it.

Upvotes: 0

Views: 90

Answers (1)

Emilio Platzer
Emilio Platzer

Reputation: 2469

If you do that you must control that the usernames aren't "reserved paths" in your system. For example if you have:

app.get('/about',...
app.get('/admin',...
app.get('/login',...

You be sure that the usernames are'n "about", "admin", "login", etc. Is preferable /user/:username like @saadq says. But if you can't, you must have a black list of usernames.

Upvotes: 1

Related Questions