【发布时间】:2014-02-22 20:41:06
【问题描述】:
我一直在使用类似的问题来尝试找到解决我遇到的问题的方法。我知道为了将 html5Mode 与 angularjs 一起使用,我需要告诉服务器如何处理对页面的直接访问。
我遇到的问题是,单击应用内的链接可以使页面正常显示,但直接访问不显示优惠。
例如
http://localhost:3001/offers/television/
应该在 routes.js 中调用
app.get('/offers', offers.all);
当链接时它会这样做
<a href="offers/television">televisions</a>
被点击
当我直接访问它时,看起来我的角度服务正在将索引页面作为资源返回......!
//Offers service used for offers REST endpoint
angular.module('mean.offers').factory("Offers", ['$resource','$routeParams',function($resource,$routeParams) {
return $resource('/offers/:offerId',
{offerId: '@_id'},
{
search: {'method': 'GET', params: {}, isArray:true}
});
}]);
我的 index.jade 头中也有 base(href="/")
角度配置.js
//Setting up route
window.app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/offers/:type/',{
controller: 'OffersController',
templateUrl: 'views/offers/list.html'
}).
when('/', {
templateUrl: 'views/index2.html'
}).
otherwise({
redirectTo: '/'
});
}
]);
//Setting HTML5 Location Mode
window.app.config(['$locationProvider',
function($locationProvider) {
$locationProvider.hashPrefix("!");
$locationProvider.html5Mode(true)
}
]);
express routes.js
//Offer Routes
var offers = require('../app/controllers/offers');
app.get('/offers', offers.all);
app.get('/offers/:offerId', offers.show);
//Home route
var index = require('../app/controllers/index');
app.get('/', index.render);
express.js
app.configure(function() {
// app.use('/', express.static(__dirname + '/'));
//cookieParser should be above session
app.use(express.cookieParser());
//bodyParser should be above methodOverride
app.use(express.bodyParser());
app.use(express.methodOverride());
//routes should be at the last
app.use(app.router);
app.get('/*', function(req, res) {
res.render('index');
});
...
为什么即使它应该命中 express routes.js 中的 /offers 路由,它也不返回报价?还是我在做一些奇怪的事情?
谢谢!
【问题讨论】:
-
需要更多代码。那是工厂服务,但我看不到它在哪里/如何使用。我没有看到你的角度路线。另外,我在这里没有看到
/offers/:offerId的路线。 -
app.get('/offers', offer.all);将处理 /offers/:offerId :offerId 仅在它是参数时才添加。将添加更多代码。