问题
I am working on an angular/cordova app, wherein after removing #
from Url and refreshing, the pages are not getting fetched and I keep getting error like -
Cannot GET /home/index
How can I configure node.js server to return index.html for any unmatched route?
回答1:
With express it should be quite simple:
app.get('/foo', function (req, res) {
res.render('foo');
});
app.get('/:bar', _checkAuth, function (req, res) {
res.render(req.params.bar);
});
app.get('*', function (req, res) {
res.render('index');
});
回答2:
Firstly, you shouldn't worry about the possible edit of URL, since a user cannot change the URLs while using the app. But yes, if you provide a wrong URL on any anchor
tag, $urlRouterProvider.otherwise('/')
will do that for you.
myApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/')
$stateProvider.state('home', {
url: '/',
controller: 'homeCtrl',
})
$stateProvider.state('p1', {
url: '/p1',
templateUrl: "templates/p1.html",
controller: "p1Ctrl"
})
...
})
And may I know what is the content in your index.html page?
来源:https://stackoverflow.com/questions/29345020/how-to-configure-node-js-routes-in-a-cordova-app