Node.js + Express 不使用 Jade
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7520541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js + Express without using Jade
提问by prog keys
Is it possible to use express withoutany template engine?
可以在没有任何模板引擎的情况下使用 express吗?
采纳答案by Robert Brisita
UPDATED
更新
Some might have concerns that sendFileonly provides client side caching. There are various ways to have server side caching and keeping inline with the OP's question one can send back just text too with send:
有些人可能担心sendFile只提供客户端缓存。有多种方法有服务器端缓存,并与OP的问题,一个保持直列可以发送回只是文字太发送:
res.send(cache.get(key));
Below was the original answer from 3+ years ago:
以下是 3 多年前的原始答案:
For anyone looking for an alternative answer to PavingWays, one can also do:
对于寻找 PavingWays 替代答案的任何人,也可以这样做:
app.get('/', function(req, res) {
res.sendFile('path/to/index.html');
});
With no need to write:
无需写:
app.use(express['static'](__dirname + '/public'));
回答by Marcel M.
Yes,
是的,
app.get('/', function(req, res){
res.render('index.html');
});
should just work
应该工作
回答by JGallardo
For anyone having the need to immediately use regular HTML without jade in a new express project, you can do this.
对于需要在新的 express 项目中立即使用没有 jade 的常规 HTML 的任何人,您可以这样做。
Add a index.htmlto the views folder.
将 a 添加index.html到视图文件夹。
In app.jschange
在app.js变化
app.get('/', routes.index);
to
到
app.get('/', function(req, res) {
res.sendfile("views/index.html");
});
UPDATE
更新
Use this instead. See comment section below for explanation.
改用这个。有关解释,请参阅下面的评论部分。
app.get('/', function(req, res) {
res.sendFile(__dirname + "/views/index.html");
});
回答by Rocco
You can serve static files automatically with Express like this:
你可以像这样使用 Express 自动提供静态文件:
// define static files somewhere on top
app.use(express['static'](__dirname + '/your_subdir_with_html_files'));
Actually this should be express.static(...) but to pass JSLint above version works too ;)
实际上这应该是 express.static(...) 但是在上面的版本中传递 JSLint 也可以;)
Then you start the server and listen e.g. on port 1337:
然后你启动服务器并在端口 1337 上监听:
// app listens on this port
app.listen(1337);
Express now serves static files in /your_subdir_with_html_files automatically like this:
Express 现在自动在 /your_subdir_with_html_files 中提供静态文件,如下所示:
http://localhost:1337/index.html
http://localhost:1337/index.html
回答by Emanuel Saringan
In your main file:
在您的主文件中:
app.get('/', function(req, res){
res.render('index');
});
Your index.jade file should only contain:
您的 index.jade 文件应该只包含:
include index.html
where index.html is the raw HTML you made.
其中 index.html 是您制作的原始 HTML。
回答by Dan
This is all out of date - correct answer for 3x, 4x is
这已经过时了 - 3x、4x 的正确答案是
The second answer here: Render basic HTML view?
这里的第二个答案: 呈现基本的 HTML 视图?

