0

I have the code below, and the path is really confusing. The api guide of restify do not explain much.

const restify = require('restify');

const app = restify.createServer();

app.get(/\/path\/.*/, function(req,res,next){
    console.log('regexp');
    return next(); // suppose to pass control to next handler, but nothing happended.
})

// it seems that '/path/:name' has the same meaning of /\/path\/.*/. Did I miss something?
app.get('/path/:name', function(req,res,next){
    console.log('colon')
    return next();// still not pass control to next handler
})

// those two works as expected.
app.get('/path/first', function(req,res,next){
    res.end('first');
})

app.get('/path/second', function(req,res,next){
    res.end('second');
})

app.listen(80, function () {
    console.log('Server is running');
});

so can someone explain to me the exact meaning of those path? and how can i make the next() work?

agsonoo
  • 25
  • 5

2 Answers2

0

In order to answer this question I will take you through the code and comment what is actually happening. As you already understand these routes are for GET requests to your server.

The first route is looking for any requests that come in on '/path/*'. As long as the leading '/path/' is present it will accept most values. The next() is used to restify to give access to the next handler in the chain, this is usually used in the creation of middle ware but has other uses.

app.get(/\/path\/.*/, function(req,res,next){
    console.log('regexp');
    return next();
})

This second route is similar to the first in that it accepts anything on '/path/*'. The difference here however is that anythign after the second slash '/:id' will be stored in req.params as a variable. For example hitting '/path/12' would store 12 in req.params.id. Accessing '/path/bar' would store bar in req.params.id.

app.get('/path/:name', function(req,res,next){
    console.log('colon')
    return next();
})

The other two paths are self explanatory. They take a path and act accordingly. What is it that you are trying to use next() to do?

hudsond7
  • 666
  • 8
  • 25
  • Suppose I send a request ,let's say '/path/first'. I wan't it be handled by `/\/path\/.*/`, `'/path/:name'` and `'/path/first'` in turn. I thought next() would work that way, but actually, Only the `/\/path\/.*/` handler gets executed. – agsonoo Jan 18 '16 at 01:39
  • It will use the first match that it finds. Next() as far as I am aware does not work in this manner. – hudsond7 Jan 21 '16 at 12:45
0

this gives you an idea of what next is used for ...

// log debug message on each request
server.use(function request(req, res, next) {
  logger.debug(req.method, req.url);
  return next();
});

// validate jwt
server.use(function request(req, res, next) {
  // validate web token here ... if invalid then respond 401 (unauthorised) else do a next ...
  return next();
});

app.get('/path/first', function(req,res,next){
    // return a response don't call next here
})

also, in your code you are using res.end - not familiar with that (but it might exist) - but did you mean to call res.send ??

danday74
  • 52,471
  • 49
  • 232
  • 283