在用express框架创建服务器后,发现有时在服务器端访问html代码时,浏览器不进行解析而是直接显示了html代码,查了一下原因!
在express框架中,官方推荐jade引擎,而我打开的html文件,渲染时就有可能会出现问题,不是全部都有问题,这个有待考究,说一下解决方法:
在允许跨域时设置的app.all()中,将头部res.header('content-type')注释掉:
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
// res.header("Content-Type", "application/json;charset=utf-8");
next();
});