节点。js +不用玉表达

时间:2022-08-02 18:28:19

Is it possible to use express without any template engine?

有没有可能不用任何模板引擎来使用express ?

6 个解决方案

#1


16  

UPDATED

更新

Some might have concerns that sendFile only 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的问题保持一致,您可以用send回发文本:

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:

对于任何寻找人行道的替代方案的人来说,我们也可以这样做:

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

With no need to write:

不需要写:

app.use(express['static'](__dirname + '/public'));

#2


30  

Yes,

是的,

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

should just work

应该工作

#3


7  

For anyone having the need to immediately use regular HTML without jade in a new express project, you can do this.

对于任何需要立即在新的express项目中使用常规HTML而不使用jade的人,您可以这样做。

Add a index.html to the views folder.

添加一个索引。html到视图文件夹。

In app.js change

在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"); 
});

#4


3  

You can serve static files automatically with Express like this:

您可以使用如下方式自动服务静态文件:

// 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 ;)

实际上,这应该是expression .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

http://localhost:1337/otherpage.html

http://localhost:1337 / otherpage.html

#5


2  

In your main file:

在你的主文件:

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

Your index.jade file should only contain:

你的指数。玉档案只应包括:

include index.html

where index.html is the raw HTML you made.

在索引。html是你制作的原始html。

#6


2  

This is all out of date - correct answer for 3x, 4x is

这都是过时的答案,3x 4x是正确的

The second answer here: Render basic HTML view?

这里的第二个答案:呈现基本的HTML视图?

#1


16  

UPDATED

更新

Some might have concerns that sendFile only 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的问题保持一致,您可以用send回发文本:

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:

对于任何寻找人行道的替代方案的人来说,我们也可以这样做:

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

With no need to write:

不需要写:

app.use(express['static'](__dirname + '/public'));

#2


30  

Yes,

是的,

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

should just work

应该工作

#3


7  

For anyone having the need to immediately use regular HTML without jade in a new express project, you can do this.

对于任何需要立即在新的express项目中使用常规HTML而不使用jade的人,您可以这样做。

Add a index.html to the views folder.

添加一个索引。html到视图文件夹。

In app.js change

在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"); 
});

#4


3  

You can serve static files automatically with Express like this:

您可以使用如下方式自动服务静态文件:

// 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 ;)

实际上,这应该是expression .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

http://localhost:1337/otherpage.html

http://localhost:1337 / otherpage.html

#5


2  

In your main file:

在你的主文件:

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

Your index.jade file should only contain:

你的指数。玉档案只应包括:

include index.html

where index.html is the raw HTML you made.

在索引。html是你制作的原始html。

#6


2  

This is all out of date - correct answer for 3x, 4x is

这都是过时的答案,3x 4x是正确的

The second answer here: Render basic HTML view?

这里的第二个答案:呈现基本的HTML视图?