15

我有根路由,它工作正常。我还有另一条路线 127.0.0.1:3000/dashboard 如果我只是在地址栏中键入该 url 我收到此错误:

无法获取 /dashboard

如果我创建一个指向相同网址的链接,它就可以正常工作。

如果我然后刷新该页面,我会再次收到相同的错误。

下面是我的 node.js 路线

应用程序.js

/**
* Module dependencies.
 */

var express = require('express')
  , routes  = require('./routes')
  , stats   = require('./routes/stats')
  , tests   = require('./routes/test')
  , http    = require('http')
  , util    = require('util')
  , path    = require('path');

var app = module.exports = express();

app.configure(function(){

  /*
   * Configuration
   *
   */
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');

  /*
   * Middleware definitions
   *
   */
  app.use(express.favicon());
  app.use(express.logger('dev'));
  /*
   * Error handling middleware
   */
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('shhhhhhhh, super secret'));
  app.use(app.router);

  // serves up dynamic css files
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(require('less-middleware')({ src: __dirname + '/public' }));

  // serves a static path
  app.use(express.static(path.join(__dirname, 'public')));

});

app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
    app.use(express.errorHandler());
});


/*
 * Endpoints
 */
app.get('/', routes.index);

app.get('/test', tests.get);
app.post('/test', tests.post);
app.options('/test', tests.options);

app.get('/stats/sends', stats.sends.get);
app.get('/stats/events', stats.events.get);
app.get('/stats/attempts', stats.attempts.get);
app.get('/stats/errors', stats.errors.get);
app.get('/stats/mquad', stats.mquad.get);

app.get('/partials/:name', routes.partials);
app.get('/index/landing', routes.landing);
app.get('/index/dashboard', routes.dashboard);

console.log('Env: ' + app.settings.env);
http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

路线/index.js

    exports.dashboard = function(req, res){
        res.render('dashboard');
    };

角度路线

    'use strict';
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
    config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.
        when('/', {
            templateUrl: 'partials/landing',
            controller: LandingCtrl
        }).
        when('/dashboard', {
            templateUrl: 'partials/dashboard',
            controller: DashboardCtrl
        }).
        otherwise({
            redirectTo: '/'
        });
    $locationProvider.html5Mode(true);
}]);
4

4 回答 4

12

这不起作用的原因是您的服务器没有捕获所有其他路由并将它们路由到由routes.index.

为了捕获所有其他路由并将它们路由到索引页面,以便您的 Angular 应用程序可以查看它是否与提供的 url 匹配,您需要做的就是在声明最后一条路由后添加以下行:

app.get('*', routes.index);

现在您应该能够:

  • 直接导航到 Angular.js 应用提供的 url
  • 刷新任何页面没有错误
于 2014-09-14T10:35:54.563 回答
8

这篇文章可能会有所帮助:

http://jjt.io/2013/11/16/angular-html5mode-using-yeoman-generator-angular/

简而言之:

npm install --save-dev connect-modrewrite

咕噜文件:

connect: {
  options: {
    // ...
    // Modrewrite rule, connect.static(path) for each path in target's base
    middleware: function (connect, options) {
      var optBase = (typeof options.base === 'string') ? [options.base] : options.base;
      return [require('connect-modrewrite')(['!(\\..+)$ / [L]'])].concat(
        optBase.map(function(path){ return connect.static(path); }));
    }
  }
}
于 2014-01-13T09:47:22.230 回答
4

Routeapp.get('/index/dashboard', routes.dashboard);指,http://hostname/index/dashboardwhen('/dashboard', { ... })http://hostname/dashboard.

您应该更正路线:app.get('/dashboard', routes.dashboard);

于 2013-02-05T04:52:44.740 回答
2

我建议在前端和后端使用一个非常快速的 javascript 解决方案。

Node.js

// set up our one route to the index.html file
    app.get('*', function (req, res){
    res.sendFile(path.join(__dirname+'/public/index.html'));
});

这段代码告诉本地/远程服务器主 html 在哪里,所以它可以找到其余的模板。

AngularJs

// If 404 Redirect to home
$routeProvider.otherwise( { redirectTo: '/'} );

这也很有帮助,所以永远不会去丢失的页面。

于 2015-10-20T18:21:39.240 回答