How to use egg - 10 common examples

To help you get started, we’ve selected a few egg 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 / test / fixtures / apps / app-ts-type-check / error.ts View on Github external
this.app.appCheckAny();
  }
}
new MyController();

// service
class MyService extends Service {
  async test() {
    this.ctx.locals.test.serviceLocalCheckAny();
    this.app.config.keys.serviceConfigCheckAny();
    this.app.serviceAppCheckAny();
  }
}
new MyService();

const app = new Application({ baseDir: __dirname, plugins: {}, type: 'application' });
new app.ContextHttpClient();
new app.HttpClient();

new Agent(undefined, 1123);

// test error in yadan
import {
  BaseContextClass as YadanBaseContextClass,
  Application as YadanApplication,
  Agent as YadanAgent,
} from 'yadan';

new YadanBaseContextClass();
const yadan = new YadanApplication({ baseDir: __dirname, plugins: {}, type: 'application' });
new yadan.ContextHttpClient();
new yadan.HttpClient();
github eggjs / egg / test / fixtures / apps / app-ts-type-check / normal.ts View on Github external
// custom base context class
class CustomBaseContextClass extends BaseContextClass {
  constructor(ctx: Context) {
    super(ctx);
  }

  test() {
    this.logger.info(this.ctx);
    this.logger.info(this.app.config.keys);
    this.logger.info(this.ctx.curl('http://127.0.0.1', { method: 'GET' }));
  }
}
new CustomBaseContextClass({} as Context).test();

// application
const app = new Application({ baseDir: __dirname, plugins: {}, type: 'application' });
app.logger.info('123');
app.middleware.slice(0);
app.name.substring(0);
app.on('egg-ready', () => {});
app.emit('egg-ready');
app.getLogger('test').info('123');
app.inspect();
app.listen(1002);
app.logger.info(app.locals.test);
const ctxHttpClient = new app.ContextHttpClient({} as Context);
ctxHttpClient.request('http://127.0.0.1', { method: 'GET' });
const appHttpClient = new app.HttpClient(app);
appHttpClient.request('http://127.0.0.1', { method: 'GET' });
app.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
app.logger.info(app.Service);
app.logger.info(app.Controller);
github eggjs / egg / test / fixtures / apps / app-ts-type-check / error.ts View on Github external
// service
class MyService extends Service {
  async test() {
    this.ctx.locals.test.serviceLocalCheckAny();
    this.app.config.keys.serviceConfigCheckAny();
    this.app.serviceAppCheckAny();
  }
}
new MyService();

const app = new Application({ baseDir: __dirname, plugins: {}, type: 'application' });
new app.ContextHttpClient();
new app.HttpClient();

new Agent(undefined, 1123);

// test error in yadan
import {
  BaseContextClass as YadanBaseContextClass,
  Application as YadanApplication,
  Agent as YadanAgent,
} from 'yadan';

new YadanBaseContextClass();
const yadan = new YadanApplication({ baseDir: __dirname, plugins: {}, type: 'application' });
new yadan.ContextHttpClient();
new yadan.HttpClient();
new YadanAgent(undefined, 1123);

// config
const config = {} as EggAppConfig;
github eggjs / egg / test / fixtures / apps / app-ts-type-check / normal.ts View on Github external
app.emit('egg-ready');
app.getLogger('test').info('123');
app.inspect();
app.listen(1002);
app.logger.info(app.locals.test);
const ctxHttpClient = new app.ContextHttpClient({} as Context);
ctxHttpClient.request('http://127.0.0.1', { method: 'GET' });
const appHttpClient = new app.HttpClient(app);
appHttpClient.request('http://127.0.0.1', { method: 'GET' });
app.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
app.logger.info(app.Service);
app.logger.info(app.Controller);
app.controller.test().then(() => {});

// agent
const agent = new Agent({ baseDir: __dirname, plugins: {}, type: 'agent' });
agent.logger.info('123');
agent.name.substring(0);
agent.on('egg-ready', () => {});
agent.emit('egg-ready');
agent.getLogger('test').info('123');
agent.inspect();
agent.listen(1002);
agent.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
agent.logger.info(agent.Service);
agent.logger.info(agent.Controller);

// single process mode
start({ baseDir: __dirname,ignoreWarning: true}).then(app=>{
  const port= 1002;
  app.logger.info('123');
  app.on('egg-ready', () => {});
github eggjs / egg / test / fixtures / apps / app-ts-type-check / error.ts View on Github external
import {
  BaseContextClass,
  Context,
  Application,
  Agent,
  Controller,
  Service,
  EggAppConfig,
  PowerPartial,
  Singleton,
} from 'egg';

new BaseContextClass({} as Context).ctx;

class MyController extends Controller {
  async test() {
    this.ctx.locals.test.localsCheckAny();
    this.app.config.keys.configKeysCheckAny();
    this.app.appCheckAny();
  }
}
new MyController();

// service
class MyService extends Service {
  async test() {
    this.ctx.locals.test.serviceLocalCheckAny();
    this.app.config.keys.serviceConfigCheckAny();
    this.app.serviceAppCheckAny();
github eggjs / egg / test / fixtures / apps / app-ts-type-check / normal.ts View on Github external
// agent
const agent = new Agent({ baseDir: __dirname, plugins: {}, type: 'agent' });
agent.logger.info('123');
agent.name.substring(0);
agent.on('egg-ready', () => {});
agent.emit('egg-ready');
agent.getLogger('test').info('123');
agent.inspect();
agent.listen(1002);
agent.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
agent.logger.info(agent.Service);
agent.logger.info(agent.Controller);

// single process mode
start({ baseDir: __dirname,ignoreWarning: true}).then(app=>{
  const port= 1002;
  app.logger.info('123');
  app.on('egg-ready', () => {});
  app.emit('egg-ready');
  app.getLogger('test').info('123');
  app.inspect();
  app.listen(port);
  app.logger.info(app.locals.test);
  const ctxHttpClient = new app.ContextHttpClient({} as Context);
  ctxHttpClient.request('http://127.0.0.1', { method: 'GET' });
  const appHttpClient = new app.HttpClient(app);
  appHttpClient.request('http://127.0.0.1', { method: 'GET' });
  app.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
  app.logger.info(app.Service);
  app.logger.info(app.Controller);
  app.controller.test().then(() => {});
github Nirongxu / nodePlatform-eggjs / server / app / service / admin / user.js View on Github external
/**
 * Created by WebStorm.
 * User: nirongxu
 * Date: 2019-03-20
 * Description: 文件描述
 */
const Service = require('egg/index').Service;

class UserService extends Service {
    // 修改用户信息
    async editUserInfo(options) {
        let {id} = options
        options.status = options.status ? "1": "0"
        let results
        await this.ctx.model.SystemUser.update(options, {
            where: {
                id//查询条件
            }
        }).then(ok => {
            console.log(ok);
            results = {
                code: 200,
                message: '修改成功'
github Nirongxu / nodePlatform-eggjs / server / app / service / admin / role.js View on Github external
/**
 * Created by WebStorm.
 * User: Administrator
 * Date: 2019/4/26
 * Description: 文件描述
 */
const Service = require('egg/index').Service;

class RoleService extends Service {
    // 获取角色列表
    async getRoleList() {
        let {ctx} = this
        let result
        await this.ctx.model.SystemRole.findAndCountAll().then(async res => {
            result = res
            for (let i = 0; i < result.rows.length; i++) {
                if (result.rows[i].name === '超级管理员') {
                    result.rows[i].dataValues.disabled = true
                }
            }

        }).catch(err => {
            console.log(err);
github chenliang2016 / CLReactAntDesign / eggserver / app / controller / upload.js View on Github external
const path = require('path');
const sendToWormhole = require('stream-wormhole');
const Controller = require('egg').Controller;
const fs = require('fs');
const mkdirp = require('mkdirp');

class UploaderController extends Controller {
  async upload() {
    const ctx = this.ctx;
    const stream = await ctx.getFileStream();
    const name = path.basename(stream.filename);

    let now = new Date();
    let today = path.join(now.getFullYear().toString(), (now.getMonth() + 1).toString(), now.getDay().toString(),"/");
    let folder = path.join(this.app.config.upload.localFilePrex, today);
    // let filename = now.getTime() + '__' + name;
    let filename = now.getTime() + '.jpg';

    try {
github TeanBlog / TeanBlog / app / controller / api / admin.js View on Github external
'use strict';

const jwt = require('jsonwebtoken');
const ms = require('ms');
const Controller = require('egg').Controller;

class AdminController extends Controller {
  // 管理员登录
  async login() {
    const { ctx } = this;
    const model = ctx.request.body;

    const isAuthenticated = await ctx.service.admin.findByLogin(model);

    if (isAuthenticated) {
      let cookieMxAge = ms('1d');
      let jwtMxAge = Math.floor(Date.now() / 1000) + (60 * 60 * 24);

      if (model.rememberMe === 'true') {
        cookieMxAge = ms('30d');
        jwtMxAge = Math.floor(Date.now() / 1000) + (60 * 60 * 30 * 24);