Express:根据路径/文件设置内容类型? [英] Express: Setting content-type based on path/file?
本文介绍了Express:根据路径/文件设置内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
Connect 将自动设置内容类型,除非您自己明确设置。这是代码片段。它使用 mime.lookup 和mime.charsets.lookup
// mime type
type = mime.lookup(path);
//< SNIP> ....
//头字段
if(!res.getHeader('content-type')){
var charset = mime.charsets.lookup(type);
res.setHeader('Content-Type',type +(charset?'; charset ='+ charset:''));
}
如果这不适合您,请将您的代码发布为您的自定义代码可能会以某种方式干扰默认行为。
I know Express has the res.contentType() method, but how to set automatically content type based on path/file (including static content)?
解决方案
Connect will automatically set the content type, unless you explicitly set it yourself. Here's the snippet that does it. It uses mime.lookup and mime.charsets.lookup
// mime type
type = mime.lookup(path);
//<SNIP>....
// header fields
if (!res.getHeader('content-type')) {
var charset = mime.charsets.lookup(type);
res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
}
If this isn't working for you, post your code as your custom code is likely interfering with the default behavior somehow.
这篇关于Express:根据路径/文件设置内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文