Koa-like understanding
Koa is a lightweight, robust and expressive nodejs framework created by the original express team.
Use Koa
- koa cheap
* $ npm i koa
- A simple koa server
const Koa = require("koa");
//koa正常使用
let app = new Koa();
//使用 会有一个回调函数
ctx: context对象
app.use(async (ctx) => {
//ctx.request === req; ctx.response === res;
// ctx.response.body = "hello world";
ctx.body = "hello "
console.log(ctx.url);
})
- Koa uses middleware to control "upstream" and call "downstream";
- Koa is an object containing a set of middleware functions; the functions in app.use can be understood as middleware
- Transfer control to another middleware through next();
let m1 = async function(ctx, next) {
console.log("m1");
//ctx.state 传递数据
//它就像一个中间间的贡献空间
ctx.state = {
perPage: 5,
totalPage: "13"
}
//将控制权,交给下一个中间件
next(); // ----->m2
// console.log("m1 end");
}
let m2 = async function(ctx, next) {
// console.log("m2");
console.log(ctx.state);
ctx.state = {
p: 1,
id: 14,
totalPage: 3
}
next(); //------->下一个中间件
// console.log("m2 end");
}
let m3 = async function(ctx, next) {
// console.log("m3");
// next();
// console.log("m3 end");
console.log(ctx.state);
ctx.body = "hello";
//状态码
// ctx.status = 404
}
// 中间件在实际使用,全部都是模板
app.use(m1);
app.use(m2);
app.use(m3);
Koa commonly used middleware
Also router
installation
npm i koa-router -S
use
const KOa = require("koa");
const Router = require("koa-router");
let app = new KOa();
let router = new Router();
router.get("/", async ctx => {
//会将 / 和 /index的请求,返回同一页面
ctx.redirect("/index");
ctx.body = "hello world";
})
router.get("/datail", async ctx => {
ctx.body = "详细页";
})
router.get("/getData", async ctx => {
// let indexData = fs.readFile("index.html");
let indexData = "w我是读取模板";
//简写
// ctx.body = {
// indexData
// }
ctx.render("/index2", {
indexData
})
})
app.use(router.routes());
app.listen(8889);
RESTful: Interface design principles
post/get
wrong approach
localhost:8000/adduser
localhost:8000/deleteuser
localhost:8000/uodateuser
localhost:8000/getuserCorrect approach
localhost:8000/user request method get get
localhost:8000/user delete delete
localhost:8000/user put update
localhost:8000/user post add
REST design generally meets the following conditions:
programs or applications should be abstracted as resources.
Each resource corresponds to a unique URI (uri is a uniform resource identifier).
Use a unified interface to operate
on the resource. Various operations on the resource will not change the resource. identifies
all operations are stateless
koa-views
- A dress koa-views
npm i koa-views -S
- use
const Views = require("koa-views");
app.use(Views(__dirname + "/views", {
extension: "pug"
}));
router.get("/", async ctx => {
// ctx.body = "hello world";
})
app.use(router.routes());
app.listen(8081);
koa-static
- koa-static is a middleware for loading static resources, through which static resources such as css and js are loaded;
- Anthropomorphic koa-static
npm i koa-static
Use koa-static
const static = require("koa-static);
app.use(static(__dirname+"/static))//加载静态文件的目录