[英]Node.js Express rendering any *.jade
I have a test node.js server running the following code: 我有一个运行以下代码的测试node.js服务器:
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.
我想知道最佳选择是基于url选择.jade文件而不进行硬编码的最佳实践是什么,有点像使用express.static的html文件。 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: 利用splats,您可以执行以下操作:
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.