How to configure node.js routes in a cordova app

元气小坏坏 提交于 2019-12-11 08:32:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!