Weiheng Li Weiheng Li - 1 year ago 52
HTML Question

What is :id means in directory of file in nodejs

I have one nodejs app, in which one section of routes file has such code:

app.get('/summary/:id', function(req, res) {
Session.findOne({_id: req.params.id}, function(err, result) {
if (err) {
res.render('summary.ejs', {
data: result._id,
name : result.name

From this code, I can see any website with valid ID in the end of address, like http://localhost:8080/summary/57df24b26b79ea95037cc3a3, etc. After searching a long time, I still don't know about the format like
. Also, the
here seems doesn't appear again. So, how does this works? I tried to find some API or document to describe, but failed.

I'd appreciate if you can help me to figure out.

Answer Source

As far as Node is concerned, that is just a string.

It looks like you are using Express, in which case, the documentation explains:

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.


app.get('/users/:userId/books/:bookId', function(req, res) {

Also, the id here seems doesn't appear again

It does: req.params.id

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download