bykof / cordova-plugin-webserver

A webserver plugin for cordova
Other
123 stars 50 forks source link

Not work with ionic2 #4

Closed oenlighteno closed 6 years ago

oenlighteno commented 6 years ago

When i test with pure cordova it work, but when i test with ionic cordova it not work. How to fix this?

bykof commented 6 years ago

Okay I have tried it now. Sorry for the long delay.

  1. I started a new project with ionic start and choosed "blank template".
  2. Install the plugin: ionic cordova plugin add https://github.com/bykof/cordova-plugin-webserver
  3. Open main.ts and type at the end of the file
    
    const webserver = window['plugins']['webserver'];

webserver.onRequest( function(request) { console.log("O MA GAWD! This is the request: ", request);

    webserver.sendResponse(
        request.requestId,
        {
            status: 200,
            body: '<html>Hello World</html>',
            headers: {
                'Content-Type': 'text/html'
            }
        }
    );
}

);

webserver.start();



The most important thing is `const webserver = window['plugins']['webserver'];`
Because ionic 2 is using typescript and you have to access the plugin over the window object.
oenlighteno commented 6 years ago

Thank you for your tryed. Thank you so much Do you mean end of file, that mean outer class bracket?

เมื่อ 29 พ.ย. 2017 6:00 หลังเที่ยง "Michael Bykovski" < notifications@github.com> เขียนว่า

Okay I have tried it now. Sorry for the long delay.

  1. I started a new project with ionic start and choosed "blank template".
  2. Install the plugin: ionic cordova plugin add https://github.com/bykof/cordova-plugin-webserver https://github.com/bykof/cordova-plugin-webserver
  3. Open main.ts and type at the end of the file

webserver.onRequest( function(request) { console.log("O MA GAWD! This is the request: ", request);

  webserver.sendResponse(
      request.requestId,
      {
          status: 200,
          body: '<html>Hello World</html>',
          headers: {
              'Content-Type': 'text/html'
          }
      }
  );

} ); webserver.start(); //... after a long long time// stop the serverwebserver.stop();

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bykof/cordova-plugin-webserver/issues/4#issuecomment-347825993, or mute the thread https://github.com/notifications/unsubscribe-auth/AdL0CaMjYfUWPStxN19GNJNMw2lwFmndks5s7TlIgaJpZM4QZ0TR .

bykof commented 6 years ago

I mean, that you cant access webserver in typescript by default. You have to save it with this statement:

const webserver = window['plugins']['webserver'];
oenlighteno commented 6 years ago

ok thank you.

2017-11-29 19:15 GMT+07:00 Michael Bykovski notifications@github.com:

I mean, that you cant access webserver in typescript by default. You have to save it with this statement:

const webserver = window['plugins']['webserver'];

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bykof/cordova-plugin-webserver/issues/4#issuecomment-347843034, or mute the thread https://github.com/notifications/unsubscribe-auth/AdL0Cd_R0Kshq8d1fVTfNc5PhLZRGgYxks5s7Ur1gaJpZM4QZ0TR .

NickStrupat commented 6 years ago

i get

Runtime Error
undefined is not an object (evaluating 'window['plugins']['webserver']')

when i am trying to access window['plugins']['webserver'] in main.ts

waidp521 commented 6 years ago

@NickStrupat i get the same error 2018-04-23 5 44 21

@bykof please help me to solve this, i was confused many days. thank you

bykof commented 6 years ago

Have you tried this: https://stackoverflow.com/questions/45080622/ionic-how-to-access-a-cordova-plugin @waidp521 @NickStrupat

saberprashant commented 6 years ago

Hey @bykof @oenlighteno @NickStrupat , still it isn't working with ionic 2. I tried mostly everything, but nothing starts. I get some errors on inspecting it, saying that plugin isn't installed, but I correctly installed it with your repo and directly as well.
Please help.