Node.js res.render(';index.html';,{layout:null})生成错误
下面是演示项目和博客文章 他正在使用Node.js res.render(';index.html';,{layout:null})生成错误,node.js,Node.js,下面是演示项目和博客文章 他正在使用jade引擎,而我不想使用它,而是使用Angularjs模板和路由 根文件夹是 client > js (contain all js files) > views > partials > html files > index.html Error: Cannot find module 'html' at Function.Module._resolveFilename (module.j
jade引擎
,而我不想使用它,而是使用Angularjs模板和路由
根文件夹是
client > js (contain all js files)
> views > partials > html files
> index.html
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.render('index.html',{layout:null})代码>刷新页面时出现错误
错误:
client > js (contain all js files)
> views > partials > html files
> index.html
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中定义的root(/)
需要:即使刷新浏览器,我也希望发送响应或不在同一页面上
{
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),那么您希望使用而不是。Render将查找与文件扩展名(如.jade)匹配的模板引擎,然后在其中运行文件。在本例中,它假定必须有一个名为html的呈现引擎,但实际上没有。SendFile将简单地按原样传输带有适当头的文件
编辑:
我仍然不是100%确定你在问什么,但我想你说的是,如果人们没有登录,你希望你的通配符路由将他们重定向到主页,但是如果他们没有登录,那么让其他路由接管
如果是这种情况,您只需要检查/*
路由的“中间件”功能
而不是:
function(req, res) {
res.redirect('/');
}
使用某种类型的条件逻辑:
function (req, res, next) {
if (/* !req.cookie.something or whatever */)
res.redirect('/');
else
next(); // continue on to the next applicable (matching) route handler
}
这样,您就不会总是陷入重定向循环。显然,如果需要,上述条件逻辑可以是异步的
我仍然相信,res.sendfile
是您问题的另一部分的正确答案,正如github成员所建议的那样。或者使用express.static
中间件,而不是res.sendfile
@bret copeland,res.redirect(req.url);创建循环。我不想使用res.sendfile。已经在使用express.static了。@Rashid对不起,我看错了。我想你问过如何发送一个重定向来刷新页面(这有时很有用)。如果您已经在使用express.static来提供html页面,那么我不确定您为什么要使用res.render,或者您的问题到底是什么?也许可以帮助我理解“即使我刷新浏览器,我也希望发送响应或不在同一页面上”是什么意思。另外,需要指出的是,res.sendfile和express.static并不等同。在许多情况下,直接调用sendfile仍然是合适或必要的。@bret copeland,谢谢您的回复。下面是我正在研究的Github上的应用程序,它使用的是Jade引擎。我想改用html。这里是讨论的问题。看看你有什么解决办法。