ise*_*sea 15 node.js angularjs
我有根路线,它工作正常.我还有另一条路线127.0.0.1:3000/dashboard如果我只是在地址栏中键入该URL我收到此错误:
无法GET /仪表板
如果我创建一个指向同一个URL的链接,它可以正常工作.
如果我然后刷新该页面,我再次得到相同的错误.
下面是我的node.js路由
app.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'));
});
Run Code Online (Sandbox Code Playgroud)
路线/ index.js
exports.dashboard = function(req, res){
res.render('dashboard');
};
Run Code Online (Sandbox Code Playgroud)
角度路线
'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);
}]);
Run Code Online (Sandbox Code Playgroud)
Gor*_*dyD 12
这不起作用的原因是您的服务器没有捕获所有其他路由并将它们路由到您所服务的单页应用程序routes.index
.
为了捕获所有其他路由并将它们路由到索引页面,以便您的角度应用程序可以查看它是否与提供的URL匹配,您需要做的就是在声明最后一个路径后添加以下行:
app.get('*', routes.index);
Run Code Online (Sandbox Code Playgroud)
现在你应该能够:
本文可能有所帮助:
http://jjt.io/2013/11/16/angular-html5mode-using-yeoman-generator-angular/
简而言之:
npm install --save-dev connect-modrewrite
Run Code Online (Sandbox Code Playgroud)
Gruntfile:
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); }));
}
}
}
Run Code Online (Sandbox Code Playgroud)
路线app.get('/index/dashboard', routes.dashboard);
指的是http://hostname/index/dashboard
,而when('/dashboard', { ... })
指的是http://hostname/dashboard
。
您应该更正路线:app.get('/dashboard', routes.dashboard);
归档时间: |
|
查看次数: |
24782 次 |
最近记录: |