nodejs background series-fourth-koa (two)

1. koa-combine-routers

1. Initialize the project

npm init -y

2. Install koa related dependencies

 npm install koa koa-router koa-body @koa/cors koa-json -S

3. The middleware koa-combine-routers you need to learn

Its function is to divide the routing file by business.
https://www.npmjs.com/package/koa-combine-routers
create a new src directory and create a new file index.js to
install this middleware as a dependency in the production process

npm install koa-combine-routers -S

4. Divide routing modules according to functional modules

Insert picture description here
Insert picture description here
Insert picture description here
Then use the
Insert picture description here
code address in index.js :
https://gitee.com/ling-xu/koa-1

Second, a security middleware koa-helmat

https://www.npmjs.com/package/koa-combine-routers
His role is to add a secure header to make the node server by default more secure.
The method of use is after npm is installed, app.use can be:
Insert picture description here

Third, the middleware koa-static for hosting static resources

1, npm i koa-static

2. Create a new static resource folder public

3. Write code

Insert picture description here

4. After running the server, you can access the static resources in the browser:

It should be noted that the root path is this public folder:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42349568/article/details/114901283