W
W
wtpltd2020-01-13 23:53:10
JavaScript
wtpltd, 2020-01-13 23:53:10

How to correctly implement the launch of Koa with the preliminary initialization of the application?

Before launching the Koa application (which should not be so important), I want to initialize the environment in which the application will work. For example, create a default user, set up a database, check the necessary directories, and so on. For example like this:

(async () => {
  await Promise.all([createDefaultUser()]); // тут всякое инициализирующее
})()
  .then(() => {
    logger.info(`Start serve on ${config.appHost}:${config.appPort}`);
    const server = app.listen(config.appPort, config.appHost);
  })
  .catch(err => {
    logger.error(`App initialization error: ${err}`);
  });

However, I still need to export server :
module.exports = server; // надо для нужд тестирования

And here's the stupor - either I can initialize the application before the server starts, but then I can't export, or I can export the server, but then the bolt is on initialization before the launch.
How is this done correctly?
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question