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
Post a Comment