【问题标题】:Static routing not working in ExpressJS静态路由在 ExpressJS 中不起作用
【发布时间】:2014-02-27 03:35:25
【问题描述】:

我有一个小应用程序,但静态路由有问题:

这是我的 app.js 配置:

employeesApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/employees', {
                templateUrl: '/partials/employees_list.html',
                controller: 'EmployeesCtrl'
            }).
            when('/employees/add_new', {
                templateUrl: '/partials/employess_add_new.html',
                controller: 'EmployeeAddNewCtrl'
            }).
            when('/employees/:employeeId', {
                templateUrl: '/partials/employees_details.html',
                controller: 'EmployeeDetailsCtrl'
            }).
            otherwise({
                redirectTo: '/employees'
            });
    }]);

控制器:

EmployeesControllers.controller('EmployeeAddNewCtrl', ['$scope',
    function($scope) {

    }]);

服务器端:

var express = require('express');
var app = express();
var mongoose = require('mongoose');

// mongodb connection and app configuration
mongoose.connect('mongodb://localhost/test');

app.configure(function () {
    app.use(express.static(__dirname + '/app'));
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
});


// application
app.get('*', function(req, res) {
    res.sendfile('./app/index.html'); // load the single view file, angular will handle other views
});

主要问题是第一条(/employees)和第三条路线(/employees/:employeeId')工作正常,但第二条路线('/employees/add_new')不起作用,我最终得到应用程序不断为页面调用服务器,我的浏览器占用了整个内存并崩溃。

这是我的文件层次结构:

Application
---app
------css
------js
--------------app.js
------partials
--------------employees_list.html
--------------employees_add_new.html
--------------employees_details.html
------lib
---server.js

【问题讨论】:

  • 错字(employess_add_new.html)?
  • 尝试将<base href="/"> 添加到您的<head></head> 中。但可能是 robertklep 提到的错字。
  • 你有app.use(app.router);吗?
  • 谢谢@robertklep,这是一个愚蠢的错字。我想我疲惫的眼睛看不到它:)

标签: node.js angularjs express angularjs-routing


【解决方案1】:

这绝对是一个错字

       `when('/employees/add_new', {
            templateUrl: '/partials/employess_add_new.html',
            controller: 'EmployeeAddNewCtrl'
        }).`

应该是employees

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2018-11-08
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多