javascript - How to add new defined routes in express js dynamically? -


in express app, i'm trying add new defined routes dynamically during runtime. here code:

app.post('/new-user', function(req,res){     var temp = { name: "joe-bloggs", age: 20 };     users.push(temp); // users global variable defined earlier     var path = '/' + temp.name;     app.get(path, function(req,res){          res.send('welcome localhost:3000/joe-bloggs');     }); }); 

so if user tried visit localhost:3000/joe-bloggs, they'll confronted welcome message, keep getting 404 not found because express telling me route hasn't been defined.

just use req.params made scenario. no need add additional routes each individual user.

app.get('/:username', function(req, res) {   res.send('welcome localhost:3000/' + req.params.username); }); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -