【发布时间】:2014-01-14 03:51:55
【问题描述】:
是否可以从同一目录提供静态内容和视图? 我在下面找到了部分解决方案:
//Enable Express static content serving:
app.use(express.static(__dirname + '/html')); //Static path is folder called html
//Also enable EJS template engine:
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/html'); //Set views path to that same html folder
app.set('view engine', 'html'); //Instead of .ejs, look for .html extension
//Start server
app.listen(8000);
//Express routes:
app.get('/', function(req,res) {
res.render('index', { message: 'hello world'});
//this only serves static index.html :(
});
app.get('/home', function(req,res) {
res.render('index', { message: 'hello world'}); //<-- this serves EJS
//whoo-hoo! serves index.html with rendered EJS 'hello world' message
});
这是完美的工作,除了不呈现 EJS 的第一条路线“/”。所有其他路由(/home、/about 等)将方便地为动态 EJS 以及静态内容提供服务。有没有办法欺骗第一个'/'以同样的方式工作?
【问题讨论】: