在Node.js / Express中,如何自动将此标题添加到每个“渲染”响应? [英] In Node.js/Express, how do I automatically add this header to every "render" response?

查看:130
本文介绍了在Node.js / Express中,如何自动将此标题添加到每个“渲染”响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多这些控制器:

  app.get('/',function(req,res) {
var stuff = {'title':'blah'};
res.render('mytemplate',stuff);
});

注意res.render?我想将此标题添加到我所做的每个响应标题中:



X-XSS-Protection:0 p>

如何自动添加响应头?

解决方案

  //全局控制器
app.get('/ *',function(req,res,next){
res.header('X-XSS-Protection',0) ;
next(); // http://expressjs.com/guide.html#passing-route control
});

只需确保这是您添加的第一个控制器,订单是重要的。


I have many of these "controllers":

app.get('/',function(req,res){
    var stuff = { 'title': 'blah' };
    res.render('mytemplate',stuff);
});    

Notice res.render? I want to add this header to every response header I make:

X-XSS-Protection: 0

How can I add that response header automatically?

解决方案

// global controller
app.get('/*',function(req,res,next){
    res.header('X-XSS-Protection' , 0 );
    next(); // http://expressjs.com/guide.html#passing-route control
});

Just make sure this is the first controller you add, order is significant.

这篇关于在Node.js / Express中,如何自动将此标题添加到每个“渲染”响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆