大家好 我是歌谣 今天给大家带来node中关于koa模块的讲解
npm init -y
npm i koa
{
"name": "koa",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"koa": "^2.14.2"
}
}
const Koa=require("koa")
const app=new Koa()
app.use((ctx,next)=>{
ctx.response.body="hello geyao"
})
app.listen(3000)
const Koa=require("koa")
const app=new Koa()
app.use((ctx,next)=>{
if(ctx.url==="/favicon.ico") return
console.log("111111")
next()
console.log("333333")
ctx.response.body="hello geyao"
})
app.use((ctx,next)=>{
console.log("22222")
})
app.listen(3000)
const Koa = require("koa")
const app = new Koa()
app.use(async (ctx, next) => {
if (ctx.url === "/favicon.ico") return
console.log("111111")
await next()
console.log("444444")
ctx.body = "hello geyao"
})
app.use(async (ctx, next) => {
console.log("222222")
await delay(1000)
console.log("333333")
})
function delay(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time)
})
}
app.listen(3000)