2014-11-01 3 views
0

나는 세 가지 매개 변수를 전달하지만 매개 변수없이 포스트 경로를 할 경우 그것을 찾을 수없는 경우 또는이 잘 작동동적 매개 변수

app.route('/api/*/*/*') 

.get(function(req, res){ 
    var entitya = req.params['0']; 
    var entityb = req.params['1']; 
    var entity3 = req.params['2']; 
}) 

.post(function(req, res){ 
    res.send('some stuff here'); 
}); 

로 정의 할 수있는 경로가 내가 세 가지 경로를 모두 찾지 않고 경로를 찾으면 URL을 찾지 못할 것입니다.

라우트 정의에서 *를 생략하고 get 매개 변수를 동적으로 만들 수있는 방법이 있습니까? 3 매개 변수를 전달해야 할 필요가 있다면 2 또는 5를 사용할 수 있습니까?

감사합니다.

답변

0

다른 API url에 대해 routers을 바인딩 할 수 있습니다.

app.use('/api/v1/function1', router1); 
app.use('/api/v1/function2', router1); 

같은 또는, 당신은 URL 이름으로 정규 표현식에 사용할 수 있습니다

app.get(/^\/api\/([a-z0-9]+)\/([a-z0-9]+)$/, function(req,res) { 
    req.params[0]; //first match of reges 
    req.params[1]; //second match of reges 
}); 

app.post(/^\/api\/([a-z0-9]+)\/([a-z0-9]+)$/, function(req,res) { 
    req.params[0]; //first match of reges 
    req.params[1]; //second match of reges 
});