• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

eggjs/egg-core: A core Pluggable framework based on koa.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

eggjs/egg-core

开源软件地址:

https://github.com/eggjs/egg-core

开源编程语言:

JavaScript 99.8%

开源软件介绍:

egg-core

NPM version Node.js CI Test coverage Known Vulnerabilities npm download

A core Pluggable framework based on koa.

Don't use it directly, see egg.

Usage

Directory structure

├── package.json
├── app.js (optional)
├── agent.js (optional)
├── app
|   ├── router.js
│   ├── controller
│   │   └── home.js
|   ├── extend (optional)
│   |   ├── helper.js (optional)
│   |   ├── filter.js (optional)
│   |   ├── request.js (optional)
│   |   ├── response.js (optional)
│   |   ├── context.js (optional)
│   |   ├── application.js (optional)
│   |   └── agent.js (optional)
│   ├── service (optional)
│   ├── middleware (optional)
│   │   └── response_time.js
│   └── view (optional)
|       ├── layout.html
│       └── home.html
├── config
|   ├── config.default.js
│   ├── config.prod.js
|   ├── config.test.js (optional)
|   ├── config.local.js (optional)
|   ├── config.unittest.js (optional)
│   └── plugin.js

Then you can start with code below

const Application = require('egg-core').EggCore;
const app = new Application({
  baseDir: '/path/to/app'
});
app.ready(() => app.listen(3000));

EggLoader

EggLoader can easily load files or directories in your egg project. In addition, you can customize the loader with low level APIs.

constructor

  • {String} baseDir - current directory of application
  • {Object} app - instance of egg application
  • {Object} plugins - merge plugins for test
  • {Logger} logger - logger instance,default is console

High Level APIs

loadPlugin

Load config/plugin.js

loadConfig

Load config/config.js and config/{serverEnv}.js

If process.env.EGG_APP_CONFIG is exists, then it will be parse and override config.

loadController

Load app/controller

loadMiddleware

Load app/middleware

loadApplicationExtend

Load app/extend/application.js

loadContextExtend

Load app/extend/context.js

loadRequestExtend

Load app/extend/request.js

loadResponseExtend

Load app/extend/response.js

loadHelperExtend

Load app/extend/helper.js

loadCustomApp

Load app.js, if app.js export boot class, then trigger configDidLoad

loadCustomAgent

Load agent.js, if agent.js export boot class, then trigger configDidLoad

loadService

Load app/service

Low Level APIs

getServerEnv()

Retrieve application environment variable values via serverEnv. You can access directly by calling this.serverEnv after instantiation.

serverEnv description
default default environment
test system integration testing environment
prod production environment
local local environment on your own computer
unittest unit test environment

getEggPaths()

To get directories of the frameworks. A new framework is created by extending egg, then you can use this function to get all frameworks.

getLoadUnits()

A loadUnit is a directory that can be loaded by EggLoader, cause it has the same structure.

This function will get add loadUnits follow the order:

  1. plugin
  2. framework
  3. app

loadUnit has a path and a type. Type must be one of those values: app, framework, plugin.

{
  path: 'path/to/application',
  type: 'app'
}

getAppname()

To get application name from package.json

appInfo

Get the infomation of the application

  • pkg: package.json
  • name: the application name from package.json
  • baseDir: current directory of application
  • env: equals to serverEnv
  • HOME: home directory of the OS
  • root: baseDir when local and unittest, HOME when other environment

loadFile(filepath)

To load a single file. Note: The file must export as a function.

loadToApp(directory, property, LoaderOptions)

To load files from directory in the application.

Invoke this.loadToApp('$baseDir/app/controller', 'controller'), then you can use it by app.controller.

loadToContext(directory, property, LoaderOptions)

To load files from directory, and it will be bound the context.

// define service in app/service/query.js
module.exports = class Query {
  constructor(ctx) {
    // get the ctx
  }

  get() {}
};

// use the service in app/controller/home.js
module.exports = function*() {
  this.body = this.service.query.get();
};

loadExtend(name, target)

Loader app/extend/xx.js to target, For example,

this.loadExtend('application', app);

LoaderOptions

Param Type Description
directory String/Array directories to be loaded
target Object attach the target object from loaded files
match String/Array match the files when load, default to **/*.js(if process.env.EGG_TYPESCRIPT was true, default to `[ '**/*.(js
ignore String/Array ignore the files when load
initializer Function custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an options object that contain path
caseStyle String/Function set property's case when converting a filepath to property list.
override Boolean determine whether override the property when get the same name
call Boolean determine whether invoke when exports is function
inject Object an object that be the argument when invoke the function
filter Function a function that filter the exports which can be loaded

Timing

EggCore record boot progress with Timing, include:

  • Process start time
  • Script start time(node don't implement an interface like process.uptime to record the script start running time, framework can implement a prestart file used with node --require options to set process.scriptTime)
  • Application start time
  • Load duration
  • require duration

start

Start record a item. If the item exits, end the old one and start a new one.

  • {String} name - record item name
  • {Number} [start] - record item start time, default is Date.now()

end

End a item.

  • {String} name - end item name

toJSON

Generate all record items to json

  • {String} name - record item name
  • {Number} start - item start time
  • {Number} end - item end time
  • {Number} duration - item duration
  • {Number} pid - pid
  • {Number} index - item index

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors


popomore


dead-horse


fengmk2


atian25


whxaxes


gxcsoccer


killagu


iyuq


ngot


initial-wu


waitingsong


AnzerWall


army8735


njugray


JacksonTian


JimmyDaddy


XadillaX


monkindey


mattma


shaoshuai0102


zhang740


dsonet


chenbin92


hyj1991


maxming2333


supperchong


ZhangDianPeng


mosaic101

This project follows the git-contributor spec, auto updated at Wed Nov 24 2021 22:24:39 GMT+0800.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap