简体   繁体   English

Node.js + Express不使用Jade

[英]Node.js + Express without using Jade

是否可以在没有任何模板引擎的情况下使用express?

Yes, 是,

app.get('/', function(req, res){
  res.render('index.html');
});

should just work 应该工作

UPDATED 更新

Some might have concerns that sendFile only provides client side caching. 有些人可能担心sendFile只提供客户端缓存。 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 : 有多种方法有服务器端缓存,并保持在线与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'));

For anyone having the need to immediately use regular HTML without jade in a new express project, you can do this. 对于任何需要在新的快速项目中立即使用常规HTML而不使用jade的人,您可以执行此操作。

Add a index.html to the views folder. index.html添加到views文件夹。

In app.js change app.js更改

app.get('/', routes.index);

to

app.get('/', function(req, res) {
  res.sendfile("views/index.html");
});

UPDATE UPDATE

Use this instead. 请改用它。 See comment section below for explanation. 请参阅以下评论部分以获取解释

app.get('/', function(req, res) {
  res.sendFile(__dirname + "/views/index.html"); 
});

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 eg 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://本地主机:1337 / index.html的

http://localhost:1337/otherpage.html HTTP://本地主机:1337 / otherpage.html

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。

This is all out of date - correct answer for 3x, 4x is 这已经过时了 - 3x的正确答案,4x是

The second answer here: Render basic HTML view? 第二个答案在这里: 渲染基本的HTML视图?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM