Created
May 19, 2019 11:22
-
-
Save loaiabdalslam/dacdada5e1982e4f625e7a6defe39896 to your computer and use it in GitHub Desktop.
This Gist will helping you to set routes name easly in your next express project | it's just abstract if you want to create your own package .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var env="http://localhost:3000/" | |
var route='users/:id/profile/' | |
var routes=[ | |
{ | |
'name':'profile', | |
'path':'users/:id/:profile/' | |
} | |
] | |
function RouteName(route,arg){ | |
let targetRoute = routes.find(e=>e.name==route).path | |
for(var key in arg){ | |
targetRoute=targetRoute.replace(`:${key}`,arg[key]) | |
//console.log(targetRoute) | |
} | |
return targetRoute | |
} | |
console.log(env+RouteName('profile',{'id':3,'profile':'loaiabdalslam'})) | |
// output : 'http://localhost:3000/users/3/loaiabdalslam/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment