The blog system server is built, using the upper-layer framework of Koa egg.js
Egg.js is an open source Node.js server framework for enterprise-level development by Ali, and its bottom layer is constructed by Koa2.
Setting up the development environment
Globally install the scaffolding tool egg-init for egg.js
npm i egg-init -g
Copy the code
Initialize the project and install the dependency packages
Egg-init --type=simple // After the project is successfully installed, run the following command: NPM install // After the installation is complete, start the project NPM run devCopy the code
- Open the web address http://127.0.0.1:7001/ in your browser
- If hi. Egg is displayed on the page, the environment is set up successfully
Egg.js directory structure and convention specification
Egg.js directory structure, only the files used
- App: Project development file, where most of the code will be written
- Config: The project and server configuration are all performed here
- Package. json: package management and command configuration file
Egg.js directory convention specification
- In the app directory, egg requires the following files
- Controller: The files required for rendering, simple business logic, and routing configuration are written here
- Public: public folder for storing public resources
- Router.js: The routing configuration file for the project, the first file to access other than middleware
- Service: Place business logic in this folder when it is complex or interacts with a database
- Middleware: Middleware folder used to write middleware, such as routing guards
Instance as follows
// app/router.js
'use strict';
module.exports = app= > {
const { router, controller } = app;
router.get('/', controller.home.index);
router.get('/list', controller.home.list)
};
// app/controller/home.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, egg';
}
async list() {
const {ctx} = this
ctx.body = '<h2>Aweiweier192 Blog</h2>'}}module.exports = HomeController;
/ / access at this time, http://127.0.0.1:7001/list, you can see the page has changed
Copy the code
Description of RESTful API design and route configuration
RESTful rules are used for project interfaces. The common modes are as follows:
- Get (SELECT): obtains resources from the server
- Post (CREATE): submits resources to the server
- Put (UPDATE): updates server resources
- Delete (delete): deletes the specified resource in the server
Configure the API interface in egg.js
- Create two new admin backend management API and front client management API files in the Controller folder
- Create a home.js file under front to manage the API interface of the client
// app/controller/front/home.js
'use strict';
const Controller = require('egg').Controller
class HomeController extends Controller{
async index(){
this.ctx.body="Front - API interface"}}module.exports = HomeController
Copy the code
Configure the route in egg.js
- Create a new folder router in the app file
- Create front. Js and admin.js in the router file
// app/router/front.js
module.exports = app= >{
const {router,controller} = app
router.get('/front/index',controller.front.home.index)
}
// Modify the router.js total configuration file
module.exports = app= > {
require('./router/front')(app)
}
/ / http://127.0.0.1:7001/front/index at this time, if can appear front - API interface
Copy the code
Egg.js connects to the mysql database
To use the mysql database in egg.js, you need to install the egg-mysql module first
npm i egg-mysql --save
Copy the code
Configure the plug-in after the installation is complete
- Egg.js requires external modules to be configured in plugin.js
// config/plugin.js 'use strict' exports.mysql = {// whether to enable enable: true, // corresponding dependencies package: 'egg-mysql' // At this point, egg.js can support connection and use of mysql databaseCopy the code
Database Connection Configuration
- Make sure your computer or server has mysql installed
- Use the PHP Study integrated development environment
// config/config.default.js
config.mysql = {
client: {
host: 'localhost'.port: '3306'.user: 'root1'.// Configure according to your own database
password: 'root1'.database: 'react_blog' // Database name
},
app: true.agent: false
}
Copy the code
Create the database and connect
- Database name: react_blog, optional
- Create table: blog_test, field ID, name, title, introduce, content
- Fill in your own data
Query the data for the table
// app/controller/front/home.js
'use strict';
const Controller = require('egg').Controller
class HomeController extends Controller{
async index(){
// Get the user table data
let result = await this.app.mysql.get("blog_test", {})console.log(result)
this.ctx.body=result
}
}
module.exports = HomeController
/ / http://127.0.0.1:7001/default/index at this time, such as database connections can display the data in the page said
Copy the code