Response-Header defaults

422 views
Skip to first unread message

Sascha Reuter

unread,
Aug 29, 2011, 12:06:49 PM8/29/11
to expre...@googlegroups.com
Hey there,

anyone know how to set a response header globally? For instance, if you want "Cache-Control" to be set to "private" as default.

Is this possible, or do you have to set this in every route manually?

Thanks in advance,

Sascha

TJ Holowaychuk

unread,
Aug 29, 2011, 12:08:08 PM8/29/11
to expre...@googlegroups.com
look into middleware, to apply to every request do:

app.use(function(req,res,next){
  res.header('Cache-Control', 'foo bar baz');
  next()
});

-- 
TJ Holowaychuk
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

Tom

unread,
Aug 29, 2011, 2:20:28 PM8/29/11
to expre...@googlegroups.com
Hi TJ,

Isn't setting such header for every request quite inefficient? Surely setting standard default headers would be more efficient?

Tom

2011/8/29 TJ Holowaychuk <t...@vision-media.ca>

TJ Holowaychuk

unread,
Aug 29, 2011, 2:22:04 PM8/29/11
to expre...@googlegroups.com
how does that differ? you're setting the fields either way,
not sure what you mean by "standard default headers". I wouldn't
consider that inefficient at all though it just adds a bit more beef
to your response but HTTP is already a very verbose protocol 

-- 
TJ Holowaychuk

Tom

unread,
Aug 29, 2011, 2:24:37 PM8/29/11
to expre...@googlegroups.com
Well, the difference is that the middleware function has to be called on every request now. Whereas it otherwise only has to be called during initialization.

I guess it is a micro optimisation though, it's okay the way it is.

Thanks.

TJ Holowaychuk

unread,
Aug 29, 2011, 2:28:16 PM8/29/11
to expre...@googlegroups.com
the Express initialization (parsing the querystring for req.query etc) is 
middleware as well, you're interacting with the request regardless, so
you'd maybe save a few function calls by adding some built-in default
header field support, not worth breaking the abstraction IMO

-- 
TJ Holowaychuk
Reply all
Reply to author
Forward
0 new messages