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

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

Open vmarquet opened this issue 5 years ago • 0 comments

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;
			});
	}

vmarquet avatar Oct 30 '19 01:10 vmarquet