node_egg framework project deployment directory structure _

// fast initialization egg project 
// create egg directory and open the egg, can be omitted 
$ mkdir egg-&& cd egg- Example Example 

// scaffolding generate egg project 
$ npm the init = egg --type the Simple
 // installation error 
  error code ENOLOCAL 
  error Could not install from "Files \ nodejs \ node_cache \ _npx \ 18476" AS IT does not Contain A package.json File.
 // solution: 
  "Replacing the node cache path" to run the project in the following directory: 
  #npm config the SET Cache -: "\ users \ logon windows username \ AppData \ Roaming \ npm-Cache C" , Ltd. Free Join 
  "directory and - no spaces between the global". 

// installations rely 
$ npm i 

// start the project: 
 $ npm RUN dev
$ Open HTTP: // localhost: 7001 


// directory structure 
egg- Project 
├── package.json 
├── app.js (optional)                   // for custom startup initialization 
├── agent.js ( optional)                 // for custom startup initialization 
├── App
 | ├── router.js                   // used to configure URL routing rules 
│ ├── the Controller                  // used to parse the user's input, processing returns after the corresponding results, 
│ |    └── home.js 
│ ├── Service (optional)              // for writing the business logic 
│ |    └── user.js 
│ ├── Middleware (optional)           // for write middleware 
│ |   Response_time.js └──
│ ├── schedule (optional)              | ├── plugin.js                  // for timed task 
│ |    └── my_task.js 
│ ├── public (optional)               // for placing static resources index.html, css, img .... . 
│ |    └── reset.css 
│ ├── View (optional)                 // used to place the template file 
│ |    └── for home.tpl has already 
│ └── extend (optional)               // used to extend the frame 
│ ├── helper.js (optional) 
│ ├── request.js (optional) 
│ ├── response.js (optional) 
│ ├── context.js (optional) 
│ ├── application.js (optional) 
│ └── agent.js (optional) 
├── config 
// used to configure the plugins to load 
| ├── config.default .js           // used to write the configuration file 
│ ├── config.prod.js
 |    ├── config.test.js (optional)
 |    ├── config.local.js (optional)
 |    └── config .unittest.js (optional) 
└── the test                            // unit test 
    ├── Middleware
     |    └── response_time.test.js 
    └── the Controller 
        └── home.test.js 



// frame built base object 
Application, Context , Request, Response, and some objects (Controller, Service, Helper, Config , Logger) frame extension 

// Application object 
// 
// the Controller, Middleware, Helper, the Service can be accessed through the Application object this.app // the application is a global application object, in a applications will only instantiate a
   event: 
  Server: This event will only be triggered once a worker process, started after the completion of the HTTP service will HTTP server through this event those exposed to developers. 
  error: there are any abnormalities after being captured onerror plug-in will trigger an error event, the context error object and associated runtime (if there is) exposed to a developer, you can log reporting and other custom processing. 
  request and response: the application receives the request and respond to requests, respectively, will trigger the request and response events and the current request context exposed, developers can monitor these events to be logged. 
  @ Access: 
  inheritance in Controller, Service base class instance, by this access to the Application object .app. 

// Context Object 
// Object Context is a request level, the framework will mount on all of the Service Context instance, 
the most common way is to get Context instance Middleware, Controller and Service, you can through this visit to .ctx Context object 


//Request & Response objects 
// Request is a class object request, inherited from Koa.Request. HTTP Request objects encapsulate native Node.js is provided a method of acquiring a series of HTTP requests assistance common parameters. 
// the Response is a class object request, inherited from Koa.Response. Encapsulates the HTTP Response Node.js native objects, there is provided a method of setting a range of support HTTP response. 
  // Get embodiment 
  can obtain the instance Request (ctx.request) and Response (ctx.response) the current request in the Context instance.
  // App / Controller / user.js 
  class the extends the Controller {the UserController 
    the async FETCH () { 
      const {App, CTX} = the this ; 
      const ID = ctx.request.query.id; 
      ctx.response.body = app.cache.get (ID); 
    } 
  } 
  //As in the example above are equivalent ctx.request.query.id and ctx.query.id, ctx.response.body = ctx.body = and are equivalent. 
  // Note that to get the POST body should use ctx.request.body, instead ctx.body. 

// Controller Object 
// framework provides a Controller base class, and recommended that all Controller inherit from the base class implementation. The base class has the following properties Controller 
    CTX - the Context Examples of the current request. 
    App - the Application instance of the application. 
    config - the configuration application. 
    Service - to apply all service. 
    logger - the current target controller logger package.
  // App / Controller / user.js 
  const = the require the Controller ( 'Egg' ) .Controller; 
  class the extends the UserController the Controller {
     //Implement 
  } 
  module.exports = the UserController; 


// Service objects 
// framework Service provides a base class, and inherit Service recommended that all implemented in the base class. 
// the attribute of the base class and properties Service Controller base class, access to a similar fashion: 
  // App /-Service / user.js 
  const = the require Service ( 'Egg' ) .service; 
  class UserService the extends Service { 
    // Implement 
  } 
  Module1 .exports = UserService;

 

Guess you like

Origin www.cnblogs.com/JunLan/p/12535930.html