简体   繁体   English

如何防止通用路由在Express.js中的特定路由中设置标头?

[英]How do I prevent a generic route from setting headers in a specific route in Express.js?

Here are my route definitions in 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));

If I try to access generic routes, such as /webstore/user defined in the two lines above, my code works fine, however, if I try to use a specific route defined above from a route file, such as /webstore/session , I receive this error: 如果我尝试访问上面两行中定义的通用路由,例如/webstore/user ,则我的代码可以正常工作,但是,如果我尝试从路由文件(例如/webstore/session使用上面定义的特定路由,我收到此错误:

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)
    ...

I would like my API to remain flat, and not have to add an alias to remove this error. 我希望我的API保持不变,而不必添加别名以消除此错误。 How do I prevent Express from setting headers because the generic and defined routes collide? 如何防止由于通用路由和已定义路由冲突而导致Express设置标头?

Because you are trying to send second response which is not possible. 因为您正在尝试发送第二个响应,所以这是不可能的。 Check below lines and it' content. 检查以下几行及其内容。

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

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

Basically when you have this in some route: 基本上,当您在某些路线中具有此功能时:

res.send("...");

and you do this in some other middleware route 然后在其他中间件路线中执行此操作

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

you get that error. 你得到那个错误。

Add the following code to the finalize method and any 404/500 error handlers to prevent sending headers a second time. 将以下代码添加到finalize方法和任何404/500错误处理程序中,以防止第二次发送标头。 However, the code is still processing all the logic in both branches if there is session manipulation, database calls, etc. 但是,如果存在会话操纵,数据库调用等,代码仍在处理两个分支中的所有逻辑。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM