2017-12-29 42 views
-1

여기 내 경로 정의는 Express.js에 : 나는를 사용하려고하면Express.js의 특정 경로에서 일반 경로가 헤더를 설정하지 못하게하려면 어떻게해야합니까?

// Building specific routes defined by a route file 
routes.use(this.initialize.bind(this)); 
routes.use(this.isAuthenticated.bind(this)); 
routes.use(this.isAuthorized.bind(this)); 
if (Route.METHOD == 'POST') { 
    routes.use(route.post.bind(route)); 
} else { 
    routes.use(route.get.bind(route)); 
} 
routes.use(this.finalize.bind(this)); 

router.use('/webstore/' + Route.RESOURCE + (parameters.length != 0 ? '/' : '') + parameters.join('/'), routes); 

//router.use('/webstore/session', routes); 

// Building generic routes 
console.log('Creating GET route: ' + 
      '/:connectionName(webstore|localdatastore)/:objectName'); 
router.get('/:connectionName(webstore|localdatastore)/:objectName', 
    this.initialize.bind(this), this.isAuthenticated.bind(this), this.isAuthorized.bind(this), this.get.bind(this), this.finalize.bind(this)); 
console.log('Creating POST route: ' + 
    '/:connectionName(webstore|localdatastore)/:objectName'); 
router.post('/:connectionName(webstore|localdatastore)/:objectName', 
    this.initialize.bind(this), this.isAuthenticated.bind(this), this.isAuthorized.bind(this), this.get.bind(this), this.finalize.bind(this)); 

I는 위의 두 줄에 정의 된 /webstore/user로, 일반적인 경로를 액세스하려고하면, 내 코드는하지만, 잘 작동

Error: Can't set headers after they are sent. 
    at ServerResponse.setHeader (_http_outgoing.js:371:11) 
    at ServerResponse.header (./node_modules/express/lib/response.js:767:10) 
    at ServerResponse.contentType (./node_modules/express/lib/response.js:595:15) 
    at Server.finalize (./dist/server.js:1156:17) 
    at Layer.handle [as handle_request] (./node_modules/express/lib/router/layer.js:95:5) 
    ... 

내 API는 횡보,이 오류를 제거 할 때의 별명을 추가 할 필요가 없습니다 싶습니다 : 경로 파일에서 위에서 정의 된 특정 경로 등 /webstore/session, 나는이 오류가 발생합니다. 일반 경로와 정의 된 경로가 충돌하여 Express가 헤더를 설정하지 못하도록하려면 어떻게해야합니까?

답변

1

불가능한 두 번째 응답을 보내려고하기 때문에. 아래 내용과 내용을 확인하십시오.

res.send("..."); 

당신은 다른 미들웨어 경로

res.send("...."); 

당신이 오류가이 작업을 수행 :

routes.use(this.finalize.bind(this)); 

router.use('/webstore/' + Route.RESOURCE + (parameters.length != 0 ? '/' : '') + parameters.join('/'), routes); 

은 기본적으로 일부 노선이있을 때.

머리글을 다시 보낼 수 없도록 finalize 메서드와 404/500 오류 처리기에 다음 코드를 추가하십시오. 그러나 세션 조작, 데이터베이스 호출 등이있을 경우 코드는 여전히 두 가지 브랜치의 모든 로직을 처리합니다.

if (res.headersSent) { 
    return next(); 
} 
+0

죄송합니다. 당신은 올바른 길로 나를 잡았고, 곧이 문제를 해결했지만, 새해 이후에 업데이트하는 것을 잊어 버렸을 것입니다. – NobleUplift