service-startup

Wait for server ready before start.

Usage no npm install needed!

<script type="module">
  import serviceStartup from 'https://cdn.skypack.dev/service-startup';
</script>

README

Wait for server ready before start.

npm

Installation

  • npm: npm i -S service-startup
  • yarn: yarn add service-startup

Usage

You can use it like on example below. App will exit if there at least on step is failed. Check all connections before starting server:

You can create file for your services like startServices.ts

import colors from 'colors'
import starter from 'service-startup'
// databases and others...

export default starter([
  {
    name: 'PostgreSQL',
    onRun:  () => client.connect(),
  },
  {
    name: 'MongoDB',
    onRun: () => connectMongo(),
  },
  {
    name: 'Media Service',
    envBlackList: ['test'],
    onRun: async () => {
      mediaApi.setCredentials(config.media)
      await mediaApi.me.info()
    },
  },
], {
  successSymbol: colors.green('[READY]'),
})

You can use it to wait for services start at server.ts:

import startServices from './startServices'

startServices.then(() => {
  server.listen(3000, () => {
    console.log(`Listening port: 3000`)
  })
})

Api

Step interface:

interface StarterStep {
  name: string
  onRun: Function
  isRequired?: boolean
  envWhiteList?: string | string[]
  envBlackList?: string | string[]
}

Config interface:

interface StarterConfig {
  successSymbol?: string
  errorSymbol?: string
  env?: string
}