试图使用翡翠渲染html文件,但它仍然像翡翠一样处理
我已经阅读了这两篇文章,以重新定义我的问题:
i have read theses two posts regrading my issue:
http://stackoverflow.com/questions/4529586/render-basic-html-view-in-node-js-express
http://stackoverflow.com/questions/12046421/how-to-configure-express-js-jade-to-process-html-files
我的代码如下:
app.engine('.html', require('jade').__express);
app.set('views', __dirname + '/app/views');
app.set('view engine', 'html');
很明显,由于某种原因,它试图读取索引,好像它仍然是一个玉器文件,因此我得到了错误.我相信我已将其正确配置为服务器.html文件.这里有什么问题?我迷路了...
it is clear that for some reason it is trying to read index as if it was still a jade file and thus i am getting the error. i believe i configured it correct to server a .html file. what is the problem here? i am getting lost...
我试图对行进行重新排序,但仍然是相同的错误.
i have tried to reorder the lines but still same error.
但是由于某些原因,我收到此错误:
but for some reason i get this error:
Error: ....\views\index.html:4
2| <html lang="en" ng-app="squashApp">
3| <head>
> 4| <meta charset="utf-8">
5| <title>Squash Organizer</title>
6| <link rel="stylesheet" href="css/app.css"/>
7| <link rel="stylesheet" href="css/bootstrap.css"/>
unexpected token "indent"
at Object.Parser.parseExpr (C:\Users\workspace\squash\node_modules\jade\lib\parser.js:241:15)
at Object.Parser.parse (C:\Users\workspace\squash\node_modules\jade\lib\parser.js:133:25)
at parse (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:93:62)
at Object.exports.compile (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:156:9)
at Object.exports.render (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:210:15)
at Object.exports.renderFile (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:247:18)
at View.exports.renderFile [as engine] (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:232:21)
at View.render (C:\Users\workspace\squash\node_modules\express\lib\view.js:76:8)
at Function.app.render (C:\Users\workspace\squash\node_modules\express\lib\application.js:505:10)
at ServerResponse.res.render (C:\Users\workspace\squash\node_modules\express\lib\response.js:756:7)
谢谢.
此行:
app.engine('.html', require('jade').__express);
您告诉Express用玉石渲染以.html
结尾的模板.
You told express to render templates ending with .html
using jade.
有了这个:
app.set('view engine', 'html');
您告诉Express,它应该将不带扩展名的模板名称解释为以html
结尾的模板名称.
you told express that it should interpret template names without extension as ones ending with html
.
所以我的猜测是您正在尝试渲染'index'
,express将其解释为index.html
,然后按照指示进行操作,传递给玉器.
So my guess is that you're trying to render 'index'
, express interprets it as index.html
and then passes to jade as it was instructed to do.
最好将玉映射到它自己的扩展范围(.jade
是显而易见的候选者).并使用其全名呈现index.html
.
It's better to map jade to it's own extentension (.jade
is an obvious candidate). And render your index.html
using it's full name.
-
将 consolidate.js 添加到您的项目中:
var engines = require('consolidate');
告诉玉器渲染以.jade
结尾的东西.
Tell jade to render stuff ending with .jade
.
app.engine('jade', require('jade').__express);
为以.html
结尾的内容注册简单的html渲染器:
Register simple html renderer for stuff ending with .html
:
app.engine('html', function(str, options) {
return function(locals) {
return str;
};
});
告诉快递,将没有扩展名的模板渲染为玉:
Tell express to render templates with no extension as jade:
app.set('view engine', 'jade');
使用它来呈现索引:
Use this to render your index:
res.render('index.html');
和这个:
res.render('view-name-without-extension');
呈现玉模板.