1 Egg. Introduction of js
1.1 what is
Egg.js is an open source set of Nodejs enterprise application development framework of Ali Company.
Egg.js was born for enterprise-level frameworks and applications, and we hope that egg.js will spawn more upper-layer frameworks to help development teams and developers reduce development costs. Official website: eggjs.org/zh-cn/intro…
1.2 Why choose egg.js
- Reduce setup time
- Everything tends to be configured
- You can focus more on business logic
- Conventions are higher than configurations, facilitating team development and maintenance
1.3 the characteristics of
- Provides the ability to customize the upper-layer framework based on Egg
- Highly extensible plug-in mechanism
- Built-in multi-process management
- Based on Koa development, excellent performance
- The framework is stable and the test coverage is high
- Incremental development
2. Initialize the project
2.1 Egg.js can be used to quickly initialize projects using scaffolding
- Operating system: Supports macOS, Linux, and Windows
- Running environment: The LTS Nodejs version is recommended. The minimum value for Nodejs is 8.x and NPM >=6.1.0
mkdir egg-example && cd egg-example
npm init egg --type=simple
npm install
Copy the code
2.2 Then start the project
npm run dev
open http://localhost:7001
Copy the code
2.3 Directory Structure
An egg - project ├ ─ ─ package. Json ├ ─ ─ app. Js (optional) ├ ─ ─ agent. The js (optional) ├ ─ ─ app | ├ ─ ─ the router. The js │ ├ ─ ─ controller │ | └ ─ ─ home. Js │ │ ├ ─ ─ service (optional) | └ ─ ─ the user. The js │ ├ ─ ─ middleware (optional) │ | └ ─ ─ response_time. Js │ ├ ─ ─ the view (optional) │ | └ ─ ─ home. TPL │ └ ─ ─ The extend (optional) │ ├ ─ ─ helper. Js (optional) ├ ─ ─ the config | ├ ─ ─ plugin. Js | ├ ─ ─ config. The default. The js │ ├ ─ ─ config. Prod. JsCopy the code
Directory Structure description
- App /router.js is used to configure URL routing rules.
- App /controller/** is used to parse user input and return corresponding results after processing.
- App /service/** Used to write the business logic layer, optional, recommended.
- App/Middleware /** For writing middleware, optional.
- App /extend/** for framework extensions, optional.
- Config /config.{env}.js is used to write configuration files.
- The config/plugin.js command is used to configure plug-ins to be loaded.
3 Configure the development environment
3.1 Adding the vscode-eggjs extension plug-in
Vscode-eggjs plug-in realizes automatic generation of Controller, Service and Model classes, improving development efficiency. If there is no code prompt for the first installation, restart vscode.
GitHub address: github.com/eggjs/vscod…
3.2 VScode debugs the egg.js project
Configure.vscode/launch.json for VSCode and launch F5 with one click.
Json {"version": "0.2.0", "configurations": [{"name": "launch Egg", "type": "node", "request": "launch", "cwd": "${workspaceRoot}", "runtimeExecutable": "npm", "windows": { "runtimeExecutable": "npm.cmd" }, "runtimeArgs": [ "run", "debug" ], "console": "integratedTerminal", "protocol": "auto", "restart": true, "port": 9229, "autoAttachChildProcesses": true } ] }Copy the code
Startup Success Log
Another way is to use Chrome DevTools for debugging
- Directly access the DevTools address last output by the console, which is the worker after the agent, without worrying about rebooting.
- Visit Chrome ://inspect, configure the port, and then click Open Dedicated DevTools for Node to Open the debug console.