【问题标题】:Express error 500 Error: Failed to lookup view "/app/views/admin/users"Express 错误 500 错误:无法查找视图“/app/views/admin/users”
【发布时间】:2014-02-07 01:12:30
【问题描述】:

我正在使用 expressjs、yeoman 和 angular。 Yeoman 已用于设置我的客户端脚手架所以结构:

根文件夹应用程序 --> 样式 --> 意见 -->管理员 登录.html --> 脚本路由 node_modules server.js

我的 server.js 位于根级别。我正在使用 angularjs 进行渲染,但在服务器端使用了身份验证来检查用户。

当它使用 angularjs 路由时,它在第一次加载时很好。如果我然后用 url 刷新浏览器,它会返回错误:

快递 500 错误:无法查找视图“/app/views/admin/users” 在 Function.app.render (/Users/tej/Projects/ecokidsproject/node_modules/express/lib/application.js:493:17) 在 ServerResponse.res.render (/Users/tej/Projects/ecokidsproject/node_modules/express/lib/response.js:798:7) 在 /Users/tej/Projects/ecokidsproject/server.js:148:9 在回调(/Users/tej/Projects/ecokidsproject/node_modules/express/lib/router/index.js:164:37) 在参数(/Users/tej/Projects/ecokidsproject/node_modules/express/lib/router/index.js:138:11) 通过时(/Users/tej/Projects/ecokidsproject/node_modules/express/lib/router/index.js:145:5) 在 Router._dispatch (/Users/tej/Projects/ecokidsproject/node_modules/express/lib/router/index.js:173:5) 在 Object.router (/Users/tej/Projects/ecokidsproject/node_modules/express/lib/router/index.js:33:10) 在 Context.next (/Users/tej/Projects/ecokidsproject/node_modules/express/node_modules/connect/lib/proto.js:193:15) 在 Context.actions.pass (/Users/tej/Projects/ecokidsproject/node_modules/passport/lib/passport/context/http/actions.js:77:8)

配置是:

     app.configure(function(){
        app.set('port', process.env.PORT || 8500);

    app.set('view engine', 'jade');
    app.use(express.static(path.join(__dirname, '../app')));
    app.use(express.static(path.join(__dirname, '/app/views')));
    app.use(express.static(path.join(__dirname, '/app/views/admin')));

    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded());



    // app setup for authentication
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({ secret: 'ecoSecret' }));
    app.use(express.methodOverride());
    // Initialize Passport!  Also use passport.session() middleware, to support
    // persistent login sessions (recommended).
    //app.use(flash());
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);

});

app.get('/admin', function(req, res){
    console.log('get admin' + __dirname + '/app/views/admin/login.html');
    res.render('/app/views/admin/users');

});

【问题讨论】:

  • 请在您的应用模块中发布您的路由提供程序配置。
  • 我的意思是 Angular js 部分。里面有没有配置html5模式的东西?
  • 是的,它使用 locationprovider 将 htmlmode5 设置为 true
  • 好的!如果您使用 html5 模式,则必须确保您的服务器可以重写任何 url 来为您的 index.html 提供服务,您的 html5 模式在其中配置。请看这篇文章:stackoverflow.com/questions/16579404/…
  • 干杯我使用 html5 模式的唯一原因是删除出现在 url 中的哈希。例如 localhost/#/admin。但认为这是不正确的用法。

标签: node.js angularjs


【解决方案1】:

我使用了这篇文章,它很有效。

URL Rewriting with ExpressJS

Index.html 需要渲染,因为它包含所有包含文件。因此它会让 angularjs routeprovider 知道要渲染什么。

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多