简体   繁体   中英

How to configure node.js in a cordova application

This is in relation to my question about changing node.js configuration in a cordova/ionic application - How to configure node.js routes in a cordova app

I didn't get a satisfactory answer to that. I also had a chat with couple of cordova/ionic guys on their blog, but they mentioned node.js cannot be configured by using express in this case and also did not have any clue to how it can be done.

So, my question is is it at all possible to do something like changing default port or url rewriting in node.js server when working with cordova/ionic applications?

Would love to know what you guys think.

Elaborating more -

Let's say I have a couple of routes like so -

http://localhost:6001/#/home/index
http://localhost:6001/#/content/index

As I didn't want the hash in URL, I added this in my app's config section -

$locationProvider.html5Mode(true);

Which works fine and URLs don't show '#' but on refreshing in browser, now I get this error -

Cannot GET /home/index

This is why I need URL rewriting to be done on server, which I can't seem to figure out.

These are my routes in app.js -

.state('home', {
            abstract: true,
            url: "/home",
            templateUrl: "app/home/home.html"
       })

.state('home.index',{
       url: "/index",
       views: {
           "index" : {
                      templateUrl: "app/home/index.html"
                     }
              }
       })

 $urlRouterProvider.otherwise('/home/index');

As we discussed in chat, the Cordova_CLI project uses a fixed server for rapid deployment through cordova serve (or ionic serve ). This node server does not use express or other plugins to handle routing, it uses a script from the [cordova-lib][1] project.

The script provided for this development server does not take into account URL rewrites, so html5Mode would not work properly without modification of this script. It could be a fork as simple as replacing the do404 function logic to instead set filePath to /index.html .

another option would be running a separate web server (node+express or any other) and including the ionic/cordova scripts in the project.

Ideally, the production app would be running against a public server, and the Cordova_CLI server should only be an issue in dev environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM