futurepress / react-native-static-server

HTTP static file server for React Native
MIT License
360 stars 183 forks source link

`port` property should be the actual used port, not 0 #74

Open vmarquet opened 4 years ago

vmarquet commented 4 years ago
const server = new StaticServer(0)  // let server choose a random available port
await server.start()
console.log(server.port)  // will print '0'

Problem: server.port will be 0, while we want it to be the actual port that was chosen by the server. Not knowing the port were the server is running makes this whole package entirely useless.

Workaround: what I'm doing is:

const server = new StaticServer(0)
const origin = await server.start()  // 'http://localhost:63051'
const port = parseInt(origin.split(':').slice(-1)[0])

Suggested solution: update start() function to save the port

    start() {
        if( this.running ){
            return Promise.resolve(this.origin);
        }

        this.started = true;
        this.running = true;

        if (!this.keepAlive && (Platform.OS === 'android')) {
            AppState.addEventListener('change', this._handleAppStateChangeFn);
        }

        return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive)
            .then((origin) => {
                this._origin = origin;
                                this.port = parseInt(origin.split(':').slice(-1)[0])   // <= SOLUTION
                return origin;
            });
    }