简体   繁体   中英

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

I currently use Jade templates within my Node project. 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. 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 )?

You have couple of options.

  1. You can just put plain HTML in a .jade file and it'll work.

  2. Or you can setup a static router to serve HTML files directly.

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

    This way anything in your ./html-views filder will be served statically. GET /view.html will serve ./html-views/view.html

Well, these HTML files will need to interact with your nodejs server to change values say "title".

In jade - title=pageTitle (ref: http://jade-lang.com/ ) works.

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.

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').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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