res.render('index.html',{layout:null})生成错误

下面的演示项目在这里和博客文章这里 。

他正在使用我don't want使用Angularjs模板和路由的jade engine

根文件夹是

 client > js (contain all js files) > views > partials > html files > index.html 

在他的代码更改后,我坚持下面的代码

我无法发出适当的回应。

如果我使用res.render('index.html', {layout : null}); 刷新页面时出现错误

错误:

 Error: Cannot find module 'html' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) 

如果我使用res.redirect('/')比刷新页面总是发送我在app.js中定义的根(/)。

需要:即使我刷新浏览器,我也想发送响应或不在同一页面上。

 { path: '/*', httpMethod: 'GET', middleware: [function(req, res) { var role = userRoles.public, username = ''; if(req.user) { role = req.user.role; username = req.user.username; } res.cookie('user', JSON.stringify({ 'username': username, 'role': role })); //res.render('index.html', {layout : null}); //res.redirect('/'); }], accessLevel: accessLevels.public } 

如果您不使用后端模板语言(如jade),那么您要使用res.sendfile而不是res.render 。 渲染将寻找匹配文件扩展名的模板引擎(如.jade),然后通过它运行文件。 在这种情况下,它假设必须有一个叫做html的渲染引擎,但是没有。 SendFile将简单地将文件原样传输到相应的头文件中。

编辑:

我仍然不能100%确定你在问什么,但是我想你所说的是你想让你的通配符路由将人们redirect到主页,如果他们没有login,但如果他们是,然后让其他路线接pipe。

如果是这种情况,只需要在/*路由的“中间件”function中进行检查。

代替:

 function(req, res) { res.redirect('/'); } 

使用某种types的条件逻辑:

 function (req, res, next) { if (/* !req.cookie.something or whatever */) res.redirect('/'); else next(); // continue on to the next applicable (matching) route handler } 

这样,你不会总是被redirect循环捕获。 显然,如果需要,上面的条件逻辑可能是asynchronous的。

我仍然相信res.sendfile是你的问题的其他部分的正确答案,正如github成员也build议的那样。