1

express.jsでフレームワークを使用していnode.jsます。app.get()コールバック内から最初の文字列引数の値を取得しようとしています。

app.get('/example/code', function(req,res){
 console.log(firstParam); // "/example/code"
});

app.get('/example/:id', function(req,res){
 console.log(firstParam); // "/example/:id"
});

app.get('/example(s?)', function(req,res){
 console.log(firstParam); // "/example(s?)"
});

これを行う方法はありますか?ご覧のとおり、URLは正確には必要ないので、ルートの要素は何でしたか。2番目の例では、戻りたくない/example/2

4

1 に答える 1

2

コールバック内のパスは次の方法で取得できます。

req.route.path

他の情報の中でreq.route

{ path: '/example/:id',
  method: 'get',
  callbacks: [ [Function] ],
  keys: [ { name: 'id', optional: false } ],
  regexp: /^\/example\/(?:([^\/]+?))\/?$/i,
  params: [ id: '42' ] }
于 2012-08-01T04:30:47.407 に答える