How to add Content-Type headers automatically using res.render?

864 views
Skip to first unread message

Wesley de Souza

unread,
Oct 16, 2013, 11:28:41 PM10/16/13
to expre...@googlegroups.com
Whenever I use res.render, I'm assuming my template render will result an HTML, but that's not the case for every routed request.

Express does not add a Content-Type header to this response, I assume because some template engine can eventually return other stuff besides HTML, but that's not the case for most of my projects using Jade.

Is there a way of configuring Express to add this header when using the res.render?

In my quest to solve this I could only find solutions that would either require me to manually add the header before every res.render (which is not optimal), or using a middleware (which is too generic or will require additional logic outside my app structure).

Hage Yaapa

unread,
Oct 17, 2013, 12:23:37 PM10/17/13
to expre...@googlegroups.com
res.set('content-type', 'text/plain');
res.render('index', { title: 'Express' });



--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/groups/opt_out.

Paul Vencill

unread,
Oct 17, 2013, 2:21:42 PM10/17/13
to expre...@googlegroups.com

Not sure why you classify using a middleware as undesireable, it's the standard approach to things like this in express.

--
Reply all
Reply to author
Forward
0 new messages