简体   繁体   English

如何在同一节点服务器上同时呈现Jade模板和HTML文件?

[英]How can I render both Jade templates and HTML files on same Node server?

I currently use Jade templates within my Node project. 我目前在Node项目中使用Jade模板。 The setup is pretty basic: 设置非常基本:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

I'm building a new part of my site with plain HTML and the HTML templates live in a different folder than the Jade templates. 我正在使用纯HTML构建网站的新部分,并且HTML模板与Jade模板位于不同的文件夹中。 My question is, how can I set things up so that I can also have an HTML view engine that serves files from a different directory (ie: not from views )? 我的问题是,如何设置内容,以便也可以使用HTML视图引擎来提供来自其他目录的文件(即,不是来自views )?

You have couple of options. 您有几个选择。

  1. You can just put plain HTML in a .jade file and it'll work. 您可以将纯HTML放入.jade文件中,它将起作用。

  2. Or you can setup a static router to serve HTML files directly. 或者,您可以设置静态路由器以直接提供HTML文件。

     app.use(express.static('./html-views')); 

    This way anything in your ./html-views filder will be served statically. 这样,您的./html-views所有内容都将被静态投放。 GET /view.html will serve ./html-views/view.html GET /view.html将投放./html-views/view.html

Well, these HTML files will need to interact with your nodejs server to change values say "title". 好了,这些HTML文件将需要与您的nodejs服务器进行交互以更改值,例如“ title”。

In jade - title=pageTitle (ref: http://jade-lang.com/ ) works. 在jade中,title = pageTitle(ref: http ://jade-lang.com/)起作用。

However, to get same functionality while serving HTML pages, you'll have to first serve HTML page as response to a request and then make another AJAX request to make changes to the DOM. 但是,要在提供HTML页面时获得相同的功能,您必须首先将HTML页面作为对请求的响应,然后再发出另一个AJAX请求以对DOM进行更改。

To serve HTML page, you can use 'fs' to readfile HTML file content and then respond with HTML content to user request or use express' function response.sendFile('/path/to/file.html'). 要提供HTML页面,您可以使用“ fs”读取HTML文件内容,然后以HTML内容响应用户请求,或者使用express函数“ response.sendFile('/ path / to / file.html')”。

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

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