1. Directory structure

  • App folder: project development files, mainly edited during development;
  • Config folder: The configuration directory for the entire project, where the project and server configurations are set;
  • Logs folder: logs folder. In normal cases, you do not need to modify or view the contents in the folder.
  • Node_modules: The module files required by the project.
  • Run folder: Configuration files generated during the running of a project are not modified.
  • Test folder: a matching file used by tests, which will be used during tests.
  • .autod.conf.js: egg.js is a configuration file that is generated by itself and does not need to be modified.
  • Eslinttrc and eslintignore: configuration files for code formatting.
  • Gitgnore: Git Settings ignore managed configuration files.
  • Package. json: Package management and command configuration file, which is often configured.

1. App directory structure:

  • The controller folder
  • The public folder
  • router

1.1 Controller Folder

  1. Function: controller that parses user input and returns corresponding results after processing.
  2. Writing controller: All controller files must be placed in the app/ Controller directory, which can be accessed by cascading directories.
//app/controller/post.js const Contrller = require('egg').Controller; class PostController extends Controller { async create() { const { ctx, service } = this; const createRule = { title: {type: 'string'}, content: {type: 'string'}, }; // validate ctx.validate(createRule); // const author = ctx.session.userId; const req = Object.assign(ctx.request.body, {author}); Const res = await service.post.create(req); Ctx. body = {id: res.id}; // Set the response content and response status code ctx.body = {id: res.id}; ctx.status = 200; } } mudule.exports = PostController;Copy the code

We defined a PostController class using the code above, and each method in the class can be referenced as a Controller in the Router. 3. Properties (mounted on this) : The defined Controller class instantiates a new object every time a request is accessed to the server, while the Controller class in the project inherits from egg.controller:

  • this.ctx: Currently requestedContext The instance of the Context objectFrom which we can get all the conveniences encapsulated in the framework for handling the current requestProperties and methods.
  • this.app: Current applicationInstance of the Application object, through which we can get what the framework providesGlobal objects and methods.
  • this.service1. Complex data processing; 2. Reference third party), through which we can accessAbstract business layer(1. Keep Controller logic simple; 2. Independent business layer; 3. Separation of logic and presentation), equivalent tothis.ctx.service.
  • this.config: Indicates the configuration item of the application runtime. (A named configuration item overrides the configuration of the same name in the default configuration fileThat is, the prod environment loads config.prod.js and config.default.js, and config.prod.js meets and overwrites config.default.js with the same name.)
  • This. logger: a logger object that has four methods: DEBUG, info, WARN, and error. A log recorded by a Logger object is preceded by the path of the file that printed the log.

1.2 Router (Configuring Route Mapping)

//app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.post('createPost', './api/posts', controller.post.create)
}
Copy the code

1.3 Configuration File (Config)

/ / config/config. Default. Js / / method one: exports. The key = < here for custom Cookie security string > exports. The logger = {level: 'DEBUG'}; Module. exports = {logger: {dir: '/home/admin/logs/demoapp',},}; // exports = {logger: {dir: '/home/admin/logs/demoapp',},};Copy the code

2. RestFul API Interface Design (Intuitive & constrained)

  • URL: the unique identifier of Internet resources;
  • Method: Indicates what operations to perform on Internet resources.

1. Request mode and operation of constraints.

  • POST(CREATE) : Creates a resource on the server.
  • GET(READ) : Reads one or more resources from the server.
  • PUT(UPDATE) : Updates the resource on the server (the client provides the full resource after the change).
  • DELETE(DELETE) : deletes resources from the server.

Zero: problem record

  1. Use an eggnpm devStartup failure:

It is normal for the development environment to run the project, that is, NPM run dev, but NPM start will fail to start. The most likely reason is that NPM start uses egg-scripts mechanism to start the process, which will interrupt the process when error logs are encountered during the run, so as to facilitate the discovery of problems. Sometimes node Warnning will think that stderr is caught during build, so NPM start will build interrupt. The solution is to ignore stderr interrupt mechanism and add –ignore-stderr to the original command.

"scripts": {
    "start": "egg-scripts start --daemon --title=egg-server-chatroomServer --ignore-stderr",
    ...
  },
Copy the code