I have a test node.js server running the following code:

var app = require('express')();

app.set('views', __dirname + '/public');
app.set('view engine', 'jade');

app.get('/', function(req, res) {
    res.render('index.jade', {name: "Test data."});
});

app.listen(3000);

This code works find. I'm wondering what the best practices are for choosing a .jade file based upon the url without hard-coding it, kind of like you might for html files using express.static. Of course, I don't want there to be a direct path correlation either (instead assigning different routes to different directories or groups of directories.) There doesn't seem to be a whole lot of solid information on the subject. Anything would be helpful. Thanks.

有帮助吗?

解决方案

with utilization of splats you could do something like this:

    var app = require('express')();

    app.set('view engine', 'jade');

    // /foo/Sam/path/to/view.jade yield a rendering of 'path/to/view.jade' with name: 'Sam'
    app.get('/foo/:name/*.*', function(req, res) {
      res.render(req.params.join('.'), {name: req.params.name});
    });

    app.listen(3000);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top