How to use egg-core - 10 common examples

To help you get started, we’ve selected a few egg-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eggjs / egg / app / extend / context.js View on Github external
_runInBackground(scope) {
    const ctx = this;
    const start = Date.now();
    /* istanbul ignore next */
    const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
    // use app.toAsyncFunction to support both generator function and async function
    return ctx.app.toAsyncFunction(scope)(ctx)
      .then(() => {
        ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
      })
      .catch(err => {
        // background task process log
        ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);

        // emit error when promise catch, and set err.runInBackground flag
        err.runInBackground = true;
        ctx.app.emit('error', err, ctx);
      });
  },
};
github eggjs / egg / app / extend / context.js View on Github external
_runInBackground(scope) {
    const ctx = this;
    const start = Date.now();
    /* istanbul ignore next */
    const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
    // use app.toAsyncFunction to support both generator function and async function
    return ctx.app.toAsyncFunction(scope)(ctx)
      .then(() => {
        ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
      })
      .catch(err => {
        // background task process log
        ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);

        // emit error when promise catch, and set err.runInBackground flag
        err.runInBackground = true;
        ctx.app.emit('error', err, ctx);
      });
  },
};
github eggjs / egg / app / extend / context.js View on Github external
runInBackground(scope) {
    // try to use custom function name first
    /* istanbul ignore next */
    const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
    scope._name = taskName;
    this._runInBackground(scope);
  },
github eggjs / egg / app / extend / context.js View on Github external
runInBackground(scope) {
    // try to use custom function name first
    /* istanbul ignore next */
    const taskName = scope._name || scope.name || eggUtils.getCalleeFromStack(true);
    scope._name = taskName;
    this._runInBackground(scope);
  },
github eggjs / egg / lib / application.js View on Github external
'use strict';

const path = require('path');
const fs = require('fs');
const ms = require('ms');
const is = require('is-type-of');
const graceful = require('graceful');
const http = require('http');
const cluster = require('cluster-client');
const onFinished = require('on-finished');
const { assign } = require('utility');
const eggUtils = require('egg-core').utils;
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
const LOCALS = Symbol('Application#locals');
const BIND_EVENTS = Symbol('Application#bindEvents');
const WARN_CONFUSED_CONFIG = Symbol('Application#warnConfusedConfig');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const CLUSTER_CLIENTS = Symbol.for('egg#clusterClients');
const RESPONSE_RAW = Symbol('Application#responseRaw');

// client error => 400 Bad Request
// Refs: https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_event_clienterror
const DEFAULT_BAD_REQUEST_HTML = `
github eggjs / egg-mock / lib / context.js View on Github external
'use strict';

// try to use eggUtils.getCalleeFromStack
// ignore it if egg-core module not found
let eggUtils;
try {
  eggUtils = require('egg-core').utils;
  if (!eggUtils) {
    // try to support egg-core@3
    eggUtils = require('egg-core/lib/utils');
  }
} catch (_) {
  // ignore eggUtils
}

module.exports = {
  runInBackground(scope) {
    /* istanbul ignore next */
    const taskName = scope._name || scope.name || (eggUtils && eggUtils.getCalleeFromStack(true));
    if (taskName) {
      scope._name = taskName;
    }
github eggjs / egg / lib / application.js View on Github external
onClientError(err, socket) {
    // ignore when there is no http body, it almost like an ECONNRESET
    if (err.rawPacket) {
      this.logger.warn('A client (%s:%d) error [%s] occurred: %s',
        socket.remoteAddress,
        socket.remotePort,
        err.code,
        err.message);
    }

    if (typeof this.config.onClientError === 'function') {
      const p = eggUtils.callFn(this.config.onClientError, [ err, socket, this ]);

      // the returned object should like:
      //
      //   {
      //     body: '...',
      //     headers: {
      //       ...
      //     },
      //     status: 400
      //   }
      //
      // default values:
      //
      // + body: ''
      // + headers: {}
      // + status: 400
github eggjs / egg-core / test / fixtures / egg / index.js View on Github external
'use strict';

const fs = require('fs');
const path = require('path');
const rimraf = require('mz-modules/rimraf');

const eggPath = path.join(__dirname, 'node_modules/egg-core');
rimraf.sync(eggPath);
fs.symlinkSync(
  path.join(__dirname, '../../..'),
  eggPath,
  'dir'
);

const EggCore = require('egg-core').EggCore;
const EggLoader = require('egg-core').EggLoader;

class AppLoader extends EggLoader {
  loadAll() {
    this.loadPlugin();
    this.loadConfig();
    this.loadApplicationExtend();
    this.loadContextExtend();
    this.loadRequestExtend();
    this.loadResponseExtend();
    this.loadBootHook();
    this.loadCustomApp();
    this.loadMiddleware();
    this.loadService();
    this.loadController();
    this.loadRouter();
  }
github eggjs / egg-core / test / fixtures / egg / index.js View on Github external
'use strict';

const fs = require('fs');
const path = require('path');
const rimraf = require('mz-modules/rimraf');

const eggPath = path.join(__dirname, 'node_modules/egg-core');
rimraf.sync(eggPath);
fs.symlinkSync(
  path.join(__dirname, '../../..'),
  eggPath,
  'dir'
);

const EggCore = require('egg-core').EggCore;
const EggLoader = require('egg-core').EggLoader;

class AppLoader extends EggLoader {
  loadAll() {
    this.loadPlugin();
    this.loadConfig();
    this.loadApplicationExtend();
    this.loadContextExtend();
    this.loadRequestExtend();
    this.loadResponseExtend();
    this.loadCustomApp();
    this.loadMiddleware();
    this.loadService();
    this.loadController();
    this.loadRouter();
  }
}
github eggjs / egg-core / test / fixtures / egg / index.js View on Github external
'use strict';

const fs = require('fs');
const path = require('path');
const rimraf = require('mz-modules/rimraf');

const eggPath = path.join(__dirname, 'node_modules/egg-core');
rimraf.sync(eggPath);
fs.symlinkSync(
  path.join(__dirname, '../../..'),
  eggPath,
  'dir'
);

const EggCore = require('egg-core').EggCore;
const EggLoader = require('egg-core').EggLoader;

class AppLoader extends EggLoader {
  loadAll() {
    this.loadPlugin();
    this.loadConfig();
    this.loadApplicationExtend();
    this.loadContextExtend();
    this.loadRequestExtend();
    this.loadResponseExtend();
    this.loadCustomApp();
    this.loadMiddleware();
    this.loadService();
    this.loadController();
    this.loadRouter();
  }

egg-core

A core Pluggable framework based on koa

MIT
Latest version published 2 months ago

Package Health Score

78 / 100
Full package analysis