使用Express.JS发送其他http标头 [英] Send additional http headers with Express.JS

查看:96
本文介绍了使用Express.JS发送其他http标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几张用Express.JS提供的静态页面。设置很简单:

  var app = express(); 

app.configure(function(){
app.use(express.static(path.join(application_root,StaticPages)));
app.use(express .errorHandler({dumpExceptions:true,showStack:true}));
});

我希望响应包含一个附加的http标头( Access-Control-Allow-Origin :* )。应该放在哪里
我尝试了以下示例,但是当然这个标题只出现在默认页面上:

  app.get '/',function(req,res){
res.setHeader(Access-Control-Allow-Origin,*);
res.send('Hello World');
});

谢谢。

解决方案


我尝试了以下示例,但是当然,标题只出现在默认页面上。




是的,这是因为你只为 GET / 路由定义了它,而不是其他路径。您应该使用中间件。



如果您要设置所有请求的标题:

  app.configure(function(){
app.use(function(req,res,next){
res.setHeader(Access-Control-Allow-Origin *);
return next();
});
app.use(express.static(path.join(application_root,StaticPages)));
app.use(express.errorHandler({dumpExceptions:true,showStack:true}));
});

如果您只想为静态文件夹执行此操作,则没有一般的方法。您可以更改express.static(来自 connect.static )。另一种方法是匹配url,如果url匹配,请设置标题。

  app.configure(function() {
app.use(function(req,res,next){
var matchUrl ='/ StaticFolder';
if(req.url.substring(0,matchUrl.length)== = matchUrl){
res.setHeader(Access-Control-Allow-Origin,*);
}
return next();
});
app.use(express.static(path.join(application_root,StaticPages)));
app.use(express.errorHandler({dumpExceptions:true,showStack:true}));
});

注意:中间件需要在路由之前生效,换句话说,你可以'在静态中间件之后放置中间件。


I have a few static pages served with Express.JS. The setup is easy:

var app = express();

app.configure(function(){
  app.use(express.static(path.join(application_root, "StaticPages")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

I want the response to include an addittional http header (Access-Control-Allow-Origin:*). Where should it be placed? I tried the below sample, but of course the header appears only on the default page:

app.get('/', function(req, res){
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.send('Hello World');
});

Thanks.

解决方案

I tried the below sample, but of course the header appears only on the default page

Yes, that is because you defined it just for the GET / route and not for the other paths. You should use a middleware instead.

If you wish to set the header for all requests:

app.configure(function(){
  app.use(function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", "*");
    return next();
  });
  app.use(express.static(path.join(application_root, "StaticPages")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

If you just want to do it for the static folders, there is no general method. You can probably change the express.static(which comes from connect.static). Another way to do it is to match urls and set the header if the url is matched.

app.configure(function(){
  app.use(function(req, res, next) {
    var matchUrl = '/StaticFolder';
    if(req.url.substring(0, matchUrl.length) === matchUrl) {
      res.setHeader("Access-Control-Allow-Origin", "*");
    }
    return next();
  });
  app.use(express.static(path.join(application_root, "StaticPages")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

NOTE: that the middleware need to be before the routes to make effect, in other words you can't put the middleware after the static middleware.

这篇关于使用Express.JS发送其他http标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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