简体   繁体   中英

firebase how do I test if emulator is running?

I am connecting to my storage emulator with the following code:

const firebaseApp = initializeApp(appConfig.firebase)
storage = getStorage(firebaseApp)
connectStorageEmulator(storage, 'localhost', 9199)

This works fine when the emulator is running. It also works fine when the emulator is NOT running! if its not running I want to do something else like this:

if (emulatorIsConnectedAndEmulatorIsRunning) {
  // do something
} else {
  // do something else
}

How can I detect whether the emulator is running or not?

TIA

This is AN answer, not necessarily the best answer and I would appreciate other answers.

Basically, I am not just emulating storage but also cloud functions.

I've made a cloud function like this:

export const ping = functions.https.onRequest((req, res) => {
  res.json({ ping: 'pong' })
})

So in my code I just ping this endpoint. For example:

axios.get(`http://localhost:5001/${storageBucket}/us-central1/ping`)

If it responds 200 then the function emulator is running (and given my setup this also means the storage emulator is running).

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