2012-08-01 3 views
0

node.jsexpress.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을 원하지 않는다. 나는 그 경로에 대한 요소가 무엇인지를 원한다. req.route에서 다른 정보 중

req.route.path 

: 두 번째 예를 들어 나는 당신이 콜백 내부의 경로를 얻을 수 있습니다 /example/2

답변

2

을 반환하지 않으려는

{ path: '/example/:id', 
    method: 'get', 
    callbacks: [ [Function] ], 
    keys: [ { name: 'id', optional: false } ], 
    regexp: /^\/example\/(?:([^\/]+?))\/?$/i, 
    params: [ id: '42' ] } 
+0

를 나는 그것을 게시 [ github] (https://github.com/visionmedia/express/issues/1253)을 문제로 삼아 정말 빨리 답장을했습니다. 'req.route.path'가 작동합니다. 제출해 주셔서 감사합니다. – ThomasReggi

관련 문제