JVE999
JVE999

Reputation: 3517

Automatically rendering jade file as if it was an html file?

I can't find an answer to this, despite it seeming rather useful.

I would like to host a site using node.js to serve compiled jade files instead of html files. Currently, I'm using:

app.get('/', function(req, res) {
    app.use(express.static(__dirname));
});

How can I get it to find page.jade when someone types in domain.com/page? And furthermore, could I write links that way in the jade file (so a(href='page') link would link to the aforementioned page)?

Upvotes: 2

Views: 675

Answers (2)

Mukesh Soni
Mukesh Soni

Reputation: 6668

Set your path as

app.get('/:pageName')
// more code 
// then
res.render(req.params.pageName+'.jade')

req.params will contain the last part in property name pageName

Upvotes: 1

Matt Pileggi
Matt Pileggi

Reputation: 7206

Express has a number of possible options for what it calls a "view engine". In order to have it process jade files and serve them as html you must configure it to do so.

One of the easiest ways to do this, if you are starting fresh, is to simply create your project using the express command as mentioned in their guide. The default views engine is jade and the following command sets stylus as the css processor:

express --css stylus myapp

If, instead, you are configuring your own server you need to configure the views engine:

app.configure(function(){

  app.set('views', path.join(staticDir,'views'));
  app.set('view engine', 'jade');
  ... the rest of your setup ...
}

Upvotes: 0

Related Questions