Javascript Node.js中的拦截器

Javascript Node.js中的拦截器,javascript,node.js,interceptor,koa,Javascript,Node.js,Interceptor,Koa,我曾尝试在NodeJS中构建一个拦截器,但到目前为止还没有实现。我想拦截,捕捉每个请求,并添加自定义标题,从膝关节炎恢复。 我的意思是,例如,如果您使用请求承诺执行http请求,我想自动添加一个自定义头并将其传播到destiny 有人知道吗?拦截器基本上是中间产品 // before all app.use(function *(next) { this.set('x-new-header', 'value'); yield next; }); // the rest app.use(

我曾尝试在NodeJS中构建一个拦截器,但到目前为止还没有实现。我想拦截,捕捉每个请求,并添加自定义标题,从膝关节炎恢复。 我的意思是,例如,如果您使用请求承诺执行http请求,我想自动添加一个自定义头并将其传播到destiny


有人知道吗?

拦截器基本上是中间产品

// before all
app.use(function *(next) {
  this.set('x-new-header', 'value');
  yield next;
});

// the rest
app.use(routes());
<>如果你的膝关节炎应用程序像反向代理。您可以使用
管道
API将新头传播到远程服务器

// before all
app.use(function *(next) {
  this.set('x-new-header', 'value');
  yield next;
});

// the reverse-proxy
const request = require('co-request');
app.use(function *() {
  this.body = this.req.pipe(request(config));
});