Runtime Environments

EggJS provides three runtime environments: unit-test environment, local development environment, production environment

Correspondingly, EggJS provides four config file, which correspond to the running environment as follows:

Config File Running Environment
config.default.js default config for all environments
config.unittest.js unit-test environment
config.local.js local development environment
config.prod.js production environment

Configure Redis

Cluster now becomes the first class citizen of CabloyJS. In other words, the CabloyJS project is ready to be deployed in a clustered environment

The cluster feature of CabloyJS is designed based on Redis. Therefore, the connection configuration information of Redis needs to be set. Here, take local development environment as an example:

src/backend/config/config.local.js

  // redis
  const __redisConnectionDefault = {
    host: '127.0.0.1',
    port: 6379,
    password: '',
    db: 0,
  };
  const __redisConnectionDefaultCache = Object.assign({}, __redisConnectionDefault, {
    keyPrefix: `cache_${appInfo.name}:`,
  });
  const __redisConnectionDefaultIO = Object.assign({}, __redisConnectionDefault, {
    keyPrefix: `io_${appInfo.name}:`,
  });

  config.redisConnection = {
    default: __redisConnectionDefault,
    cache: __redisConnectionDefaultCache,
    io: __redisConnectionDefaultIO,
  };

  config.redis = {
    clients: {
      redlock: config.redisConnection.default,
      limiter: config.redisConnection.default,
      queue: config.redisConnection.default,
      broadcast: config.redisConnection.default,
      cache: config.redisConnection.cache,
      io: config.redisConnection.io,
    },
  };

CabloyJS implements multiple features based on Redis: limiter, queue, broadcast, cache. These features access the same Redis service by default. When the number of visits increases, separation can be considered

Configure MySQL

EggBornJS implements different database access strategies for each runtime environment:

  1. unit-test environment:Every time service started, the old test database is always deleted and a new test database is created

  2. local development environment:Every time service started, the available test database is always looked up, and if not found, a new one is created

  3. production environment:The database needs to be created manually and specified in config.prod.js

Through these database access strategies, debugging can be very convenient in the development stage. For example, the database architecture changes frequently during the development stage, when only one unit test command is needed to take effect. The basic operation is as follow:

# run unit-test
$ npm run test:backend
# start local development service
$ npm run dev:backend

Therefore, it is necessary to set up the database connection configuration of the three runtime environments separately

1. unit-test environment

src/backend/config/config.unittest.js

  // mysql
  config.mysql = {
    clients: {
      // donnot change the name
      __ebdb: {
        host: '127.0.0.1',
        port: '3306',
        user: 'root',
        password: '',
        database: 'sys', // donnot change the name
      },
    },
  };
Name Description
__ebdb the system’s built-in MySQL node name, which is not allowed to change
database specified as sys. Every time service started, the old test database is always deleted and a new test database is created

2. local development environment

src/backend/config/config.local.js

  // mysql
  config.mysql = {
    clients: {
      // donnot change the name
      __ebdb: {
        host: '127.0.0.1',
        port: '3306',
        user: 'root',
        password: '',
        database: 'sys', // recommended
      },
    },
  };
Name Description
database recommendeded as sys. Every time service started, the available test database is always looked up, and if not found, a new one is created
If specified as the other specific database name, then use this one directly

3. production environment

src/backend/config/config.prod.js

  // mysql
  config.mysql = {
    clients: {
      // donnot change the name
      __ebdb: {
        host: '127.0.0.1',
        port: '3306',
        user: 'root',
        password: '',
        database: '{{name}}',
      },
    },
  };
Name Description
database The database need to be created manually

Override Config

You can use project level config to override module level config

src/backend/config/config.default.js

// modules
config.modules = {
  'test-party': {
    message: 'Hello World!',
  },
};