为什么要在node.js中使用jade或任何其他模板

时间:2022-11-19 09:54:29

Why we use jade or any other template in node.js(express.js). We can use send method to send file html file and what is the meaning of rendering the HTML.

为什么我们在node.js(express.js)中使用jade或任何其他模板。我们可以使用send方法发送文件html文件,渲染HTML的含义是什么。

2 个解决方案

#1


1  

You can choose to directly send back a static HTML file in the filesystem if you wish, but that wouldn't be very useful if you wanted to show dynamic content.

如果您愿意,可以选择直接在文件系统中发回静态HTML文件,但如果您想要显示动态内容,这将不是非常有用。

Using templates like jade, you can easily inject data from your application logic into the returned page.

使用像jade这样的模板,您可以轻松地将应用程序逻辑中的数据注入返回的页面。

#2


-1  

Well, I would say there are actually different philosophies of design at work.

好吧,我想说实际上有不同的设计哲学在起作用。

As others have said, you can use Jade or other templating engines to render server-side variables in your HTML that gets written to the client.

正如其他人所说,您可以使用Jade或其他模板引擎在HTML中呈现写入客户端的服务器端变量。

Others would argue that this is not the best design, and that it's better to render static HTML that uses clientside JavaScript as an API client, and have your Node process only expose a RESTful API.

其他人则认为这不是最好的设计,并且最好使用客户端JavaScript作为API客户端呈现静态HTML,并让Node进程仅公开RESTful API。

Either approach works and has pro's and con's, but I think the latter is somewhat more 'modern' and flexible if you have to start thinking about other possible clients for your app (e.g. mobile native apps).

这两种方法都有效,并且具有专业性和可持续性,但我认为如果您必须开始考虑应用程序的其他可能客户端(例如移动本机应用程序),后者更具“现代性”和灵活性。

#1


1  

You can choose to directly send back a static HTML file in the filesystem if you wish, but that wouldn't be very useful if you wanted to show dynamic content.

如果您愿意,可以选择直接在文件系统中发回静态HTML文件,但如果您想要显示动态内容,这将不是非常有用。

Using templates like jade, you can easily inject data from your application logic into the returned page.

使用像jade这样的模板,您可以轻松地将应用程序逻辑中的数据注入返回的页面。

#2


-1  

Well, I would say there are actually different philosophies of design at work.

好吧,我想说实际上有不同的设计哲学在起作用。

As others have said, you can use Jade or other templating engines to render server-side variables in your HTML that gets written to the client.

正如其他人所说,您可以使用Jade或其他模板引擎在HTML中呈现写入客户端的服务器端变量。

Others would argue that this is not the best design, and that it's better to render static HTML that uses clientside JavaScript as an API client, and have your Node process only expose a RESTful API.

其他人则认为这不是最好的设计,并且最好使用客户端JavaScript作为API客户端呈现静态HTML,并让Node进程仅公开RESTful API。

Either approach works and has pro's and con's, but I think the latter is somewhat more 'modern' and flexible if you have to start thinking about other possible clients for your app (e.g. mobile native apps).

这两种方法都有效,并且具有专业性和可持续性,但我认为如果您必须开始考虑应用程序的其他可能客户端(例如移动本机应用程序),后者更具“现代性”和灵活性。