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

1.3k Views Asked by At

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)?

2

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

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