react-native-static-server icon indicating copy to clipboard operation
react-native-static-server copied to clipboard

Multiple servers?

Open jtreat3 opened this issue 4 years ago • 5 comments

Thanks for this server. Great work!

I'm wondering if it's possible to run two servers at once. So far, it looks like if I try, even though the port property of each instance is different and correct, the url returned upon starting both uses only the first port, and it seems like nothing is actually served to the second port.

My goal with two servers is to be able to serve both the assets bundled in my app as well as assets saved by the app (which seemingly must be stored in another location). Thanks.

jtreat3 avatar Sep 04 '20 21:09 jtreat3

@jtreat3 :Have you solved this issue?

JowelTisso avatar Mar 16 '21 12:03 JowelTisso

@jtreat3 In case if you have not solved this issue or anyone else... I was facing this issue cause I was trying to store the newServer instance in a useState which was not saved for some reason... So I was not able to kill it in component unmount or any other case... So I used a local variable to save the instance of newServer then i was able to kill the server from that local variable... This is all which solved my problem... Hope it helps...

JowelTisso avatar Mar 16 '21 13:03 JowelTisso

I thought I had stored it as a local variable, but it was too long ago to remember. Thanks @JowelTisso.

jtreat3 avatar Mar 16 '21 14:03 jtreat3

This package doesn't support multiple servers. The FPStaticServer (underlying native module) is a singleton, and these are the methods it exposes:

  • start
  • stop
  • isRunning

calling start multiple times is not supported:

    if(_webServer.isRunning != NO) {
        NSLog(@"StaticServer already running at %@", self.url);
        resolve(self.url);
        return;
    }

The exposed javascript API is confusing, making you believe you can instantiate multiple server instances. This is not the case. You will get the same server when you call start the second time, even though the javascript instance will appear to hold the values you've specified (_origin will be different though and the only property telling the truth).

I wish we were exposed only the FPStaticServer API. The javascript API is misleading.

noway avatar Jun 21 '21 04:06 noway

I am wondering why the _webServer is designed to be a singleton? Any platform restriction on iOS?

KennanChan avatar Apr 03 '22 04:04 KennanChan