2016-09-04 2 views
0

앱을 쓰고 있는데 들어오는 URL을 특정 "예상 된"형식으로 표준화하는 미들웨어를 만들고 싶다면 /Docs/PAGE/과 같은 경로를 /docs/page으로 변환해야합니다.특급 요청이 변경됩니다. 경로?

나중에 경로 처리 미들웨어를 작성하기 쉽게하려면이 작업을 수행하고 싶습니다. 여기

은 내가 쓴 것입니다 : 그러나

app.use(function(request, response, next) { 
    var standardisedPath = request.path.toLowerCase().replace(/\/$/, ''); 
    logger.info('Standardising original path %s to %s', request.path, standardisedPath); 
    request.path = standardisedPath; 
    next(); 
}); 

app.get(/^\/docs.*/i, function(request, response) { 
    var docPath = request.path + '.md'; 
    response.send(docPath); 
}); 

, 내가 /docs/page.md를 기대하고있어 비록 http://localhost/docs/PAGE/ 예를 들어, 내 애플, /docs/PAGE/.md 응답 방문 할 때.

+0

'standardisedPath'와'normalisedPath'이 같은 변수가 될 수 있습니다 ... – sova

+0

@sova이 시점까지 전체 파일을 찾거나 바꾸는 것을 잊어 버렸습니다. – theonlygusti

+0

'request.path'는 나를 위해 예외를 던집니다. 그 외에 브라우저에서 URL에 소문자 버전을 표시하려는 경우 리디렉션을 생성하려는 것처럼 보입니다. – robertklep

답변

1

는 리디렉션 할 것을 말해 낮은 맡았다 해당하는 그들의 경로에 적어도 하나 개의 대문자 문자를 가지고있는 URL의 :

app.use(function(request, response, next) { 
    if (/[A-Z]/.test(request.url)) { // check for at least one upper case character 
    return response.redirect(request.url.toLowerCase()); 
    } 
    next(); 
}); 
+0

좋은. response.redirect는 좋은 호출입니다. – sova