nodejs express render html没有编译
发布于 9 年前 作者 pf12345 6989 次浏览 最后一次编辑是 8 年前 来自 问答

app.js:

var express = require('express');
var http = require('http');
var path = require('path');
const session = require('express-session');
var RedisStore = require('connect-redis')(session);
var config = require('./config');
var swig = require('swig');
var methodOverride = require('method-override');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var serveStatic = require('serve-static');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var mps = require("multi-process-session");
var compression = require('compression');

var app = express();
//设置跨域访问
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();
});
app.use(cookieParser());
app.use(mps());
app.set('views', path.join(__dirname, 'views'));
//app.engine('html', require('ejs').renderFile);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
swig.setDefaults({ cache: false });
app.use(compression());

//app.use(favicon());
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride());
app.use(serveStatic(path.join(__dirname, 'public')));

require('./router/router').init(app);

http.createServer(app).listen(config.port, function(){
    console.log('Express server listening on port ' + config.port);
});

router.js:
exports.init = function(app) {
    app.get('/',function(req, res, next) {
        res.setHeader("Content-Type", "text/plain");
        res.render('home/index', {});
    });
};

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>ARTICLE</title>
    <link rel="shortcut icon" href="/images/icon/favicon1.ico"/>
    <link rel='stylesheet' href='/css/bootstrap.min.css'/>
    <link rel='stylesheet' href='/css/style.css'/>
</head>
<body>
<div class="logoWrap">
    <div class="logo"></div>
</div>
<div class="contentBox container" id="mainWrap">
</div>
<div id="msgBox"></div>
<script src="/build/js/main.js"></script>
</body>
</html>

最终结果为: untitled1.png

请问下各位这个是怎么回事?在线等啊,亲们;

项目地址:https://github.com/pf12345/article-save 可以帮忙看一下吗?我在mac和windows上面都是一样的

8 回复

app.set(‘view engine’, ‘html’);

// res.setHeader(“Content-Type”, “text/plain”);
res.setHeader("Content-Type", "text/html");

@i5ting 这个我加了,什么意思?

@William17 我加这个,也还是不行

我把项目地址放在上面了,帮我看一下哈,确实没找到什么问题了

@pf12345 你的问题问的是说为什么浏览器没有解析渲染你的html吗 ? 我的意思是叫你替换这一句 确定不行 ?

app.get('/', function(req, res, next) {
  // res.setHeader("Content-Type", "text/plain");
  res.setHeader("Content-Type", "text/html");
  res.render('home/index', {});
});

res.header(“Content-Type”, “application/json;charset=utf-8”); 这句去掉。

@William17 ok,我试一下,谢谢了

回到顶部