在路由结束后使用()

我试图使用Restify的“use()”方法来进行通用的error handling,例如,

server = restify.createServer({...}); server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); ... app.post('/test/:id', function (req, res, next) {...}) server.use(function(err, req, res, next) { logger.error({req: req}, "error processing request"); }); 

但最后的.use()处理程序在路由完成处理之后从未被调用过。 它是在航线之后“登记”的。 我可以看到服务器启动时,这个.use()被处理后,我的路线处理。 我是相当新的restify,所以我的问题是,路由结束后,可以使用.user()方法来应用处理程序。 我知道我们可以听到'after'事件来完成路由后的处理,但是想看看我们能否做.use()处理程序。

更新:这是一个简化版本的代码。 在server.post()之前注册的.use()方法被调用,但不是在server.post()之后注册的。 有什么build议么?

 var restify = require('restify'); var cookieParser = require('restify-cookies'); var helmet = require('helmet'); var util = require('util'); var logger = require('./util/logger'); module.exports = function(server){ server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); server.use(helmet.noCache()); server.use(helmet.hidePoweredBy()); server.use(cookieParser.parse); // this .use() method is called when accessing the server.post route below server.use(function(req, res, next) { console.log('BERORE ROUTE'); return next(); }); server.post('/test/:returnId', function (req, res, next) { var err = new restify.errors.InternalServerError('oh noes!'); return next(err); }); // this .use() method is not called when accessing the server.post route above server.use(function(req, res, next) { console.log('Tata...'); return next(); }); }; 

看到里面的代码会很有趣

 app.post('/test/:id', function (req, res, next) { // what is happening here? }); 

我猜你不会next调用,所以下一个中间件(使用语句中的日志logging中间件)永远不会被调用。 所以调用next()会调用你的中间件。

但是,在你的情况下,如restify文档中所述,提出错误并处理错误可能会更好:

 app.post('/test/:id', function (req, res, next) { // some internal unrecoverable error var err = new restify.errors.InternalServerError('oh noes!'); return next(err); }); app.on('InternalServer', function (req, res, err, next) { //err.body = 'something is wrong!'; logger.error({req: req}, "error processing request"); // <= from your code return next(); }); 

但这取决于你想要实现什么,以及你在app.post里面做了app.post

你最后一个“使用”处理程序永远不会被调用的原因是因为在处理程序被分配给路由时,处理程序链中不存在。 对于每个“使用”调用restify将该处理程序添加到一个处理程序数组。

当一个路由函数(get,post等)被调用时,restify会创build一个处理程序链,并被调用该路由。 每条路由的处理程序链是build立在你为该路由传入的内容之外的,并且与迄今为止通过“使用”调用find的所有处理程序一起被预处理。

所以,当你的server.post函数调用被处理时,“Tata …”处理程序在处理程序链中还不存在,因此它不会被预置。 这就是为什么所有“使用”呼叫总是放在路线上方的原因。

为了说明这一点,我在restify中的server.use函数中放置了两个console.log语句。 你可以看到与中间件处理程序链(我命名你的函数“之前”和“error_handler”,所以他们可以在输出中识别)链build设。 您可以看到最后一次调用server.use会生成一个包含所有中间件处理程序的链,但posttestreturnId路由的处理程序不包含“error_handler”,因为它在调用时不在数组中。

 [ [Function: parseAccept] ] // the handler chain {} // the routes and their handlers [ [Function: parseAccept], [Function: parseQueryString] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache], [Function: hidePoweredBy] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache], [Function: hidePoweredBy], [Function: parseCookies] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache], [Function: hidePoweredBy], [Function: parseCookies], [Function: before] ] {} [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache], [Function: hidePoweredBy], [Function: parseCookies], [Function: before], [Function: error_handler] ] { posttestreturnid: [ [Function: parseAccept], [Function: parseQueryString], [Function: readBody], [Function: parseBody], [Function: nocache], [Function: hidePoweredBy], [Function: parseCookies], [Function: before], [Function: returnId] ] } 

如果您想在其他路由不使用server.use()之后执行某个function,请使用下面的代码。

  // this .use() method is called when accessing the server.post route below server.use(function(req, res, next) { console.log('BERORE ROUTE'); return next(); }); server.post('/test/:returnId', function (req, res, next) { var err = new restify.errors.InternalServerError('oh noes!'); return next(err); }); server.on('after', function(req, res, next) { console.log('Tata...'); return next(); }); 

http://restify.com/#audit-logging所&#x793A;