node开发WEB学习 学习自廖雪峰 =>
koa
1. 上来直接一个案例 => 和之前用http模块来任何请求都返回一个值一样
用于处理请求的函数就叫做中间件 用use把中间件注册到koa上去 http的请求和响应都被封装到了ctx(上下文)上面,做了更加高级的封装和集成 多个中间件根据顺序依次push进数组里面,通过next方法把控制权交给下一个中间件,最后一个完成之后又会依次把控制权还给上一个 koa的洋葱模型
const Koa = reqiure ( 'koa' ) ;
const app = new Koa ( ) ;
app. use ( async ( ctx, next) => {
await next ( ) ;
ctx. response. type ( 'text/html' ) ;
ctx. response. boyd ( 'Hello, Koa' ) ;
} )
app. listen ( '30000' ) ;
console. log ( 'app started at port 3000...' ) ;
2. 直接运行上面那段代码是不行的,要先引入koa包
方案一:npm install koa@2.0.0
要在项目目录下面安装 方案二:新建一个pakcage.json 文件,里面写上下面的json,然后在当前目录下运行 npm install
方案二会好一点,这样即使把命令安装回来的koa包删了,只要在执行一边npm install
就可以又装回来了
{
"name" : "hello-koa2" ,
"version" : "1.0.0" ,
"description" : "Hello Koa 2 example with async" ,
"main" : "app.js" ,
"scripts" : {
"start" : "node app.js"
} ,
"keywords" : [
"koa" ,
"async"
] ,
"author" : "Michael Liao" ,
"license" : "Apache-2.0" ,
"repository" : {
"type" : "git" ,
"url" : "https://github.com/michaelliao/learn-javascript.git"
} ,
"dependencies" : {
"koa" : "2.0.0"
}
}
3. koa middleware 中间件,敲重点来了
收到http请求,koa就会去调用app.use()函数, 传入ctx和next参数 对ctx操作,设置返回的内容 awati next() 作用:koa把很多async函数组成一个处理链,然后用await next()来调用下一个
async函数 我们把每个async函数称为middleware 如果一个middleware没有调用await next(),那他就不会再执行
4. 小小的注意点
ctx.url 等于 ctx.request.url 进行了高度封装 ctx.type 等于 ctx.response.type
koa-router and koa-bodyparser 路由和request请求解析传参
1. 路由
package.json 添加依赖 "koa-router": "7.0.0"
引入路由,是一个函数 const router = require('koa-router')();
router.get(请求路径,回调函数(){ 里面就是请求的处理了 }) => eg:
router. get ( '/hello' , async ( ctx, next) => {
let name = ctx. params. name;
ctx. response. body = `<h1>hello, ${ name} </h1>`
} ) ;
监听端口之前记得写上 路由中间件 app.use(router.routers())
2. request解析
package.json 添加依赖 "koa-bodyparser": "3.2.0"
post请求发送的json,表单,会作为request的body发送过来,需要bodyparser来解析 引入模块 const bodyParser = require('koa-bodyparser')
然后把中间件添加在合适的位置 app.use(bodyParse())
注意: 这个koa-bodyparser必须在router之前被注册到app对象上
3.综合案例
< form action = " /signin" method = " post" >
< p> Name: < input name = " name" value = " koa" > </ p>
< p> Password: < input name = " password" type = " password" > </ p>
< p> < input type = " submit" value = " Submit" > </ p>
</ form>
const Koa = require ( 'koa' ) ;
const router = require ( 'koa-router' ) ( ) ;
const bodyParser = require ( 'koa-bodyparser' ) ;
const app = new Koa ( ) ;
app. use ( bodyParser ( ) ) ;
app. use ( async ( ctx, next) => {
console. log ( `进程 ${ ctx. request. method} ${ ctx. url} ` )
await next ( ) ;
} ) ;
router. get ( '/hello/:name' , async ( ctx, next) => {
let name = ctx. params. name;
ctx. response. body = `<h1>hello, ${ name} </h1>`
} ) ;
router. get ( '/' , async ( ctx, next) => {
ctx. response. body = `<h1>Inex Page</h1>`
} ) ;
router. post ( '/signin' , async ( ctx, next) => {
var
name = ctx. request. body. name || '' ,
password = ctx. request. body. password || '' ;
console. log ( `signin with name: ${ name} , password: ${ password} ` ) ;
if ( name === 'koa' && password === '12345' ) {
ctx. response. body = `<h1>Welcome, ${ name} !</h1>` ;
} else {
ctx. response. body = `<h1>Login failed!</h1>
<p><a href="/">Try again</a></p>` ;
}
} ) ;
app. use ( router. routes ( ) ) ;
app. listen ( 3000 ) ;
console. log ( 'app started at port 3000' ) ;