路由
# 1. 概念
# 1.1 什么是路由
建立URL与处理函数之间的对应关系 主要作用:根据不同的Module和URL返回的内容
# 1.2 ctx
http上下文: http请求 + http响应
{
request: {
method: 'GET',
url: '/',
header: {
host: 'localhost:3002',
connection: 'keep-alive',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en,zh;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7',
cookie: '_ga=GA1.1.1476456273.1652003400; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22fbb12fc717094d51862e63adfc69c0da%22%2C%22first_id%22%3A%2217fabdc3342a47-03c78852982006c-37677109-2073600-17fabdc3343143f%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%2C%22%24latest_search_keyword%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%2C%22%24latest_referrer%22%3A%22url%E7%9A%84domain%E8%A7%A3%E6%9E%90%E5%A4%B1%E8%B4%A5%22%7D%2C%22%24device_id%22%3A%2217fabdc3342a47-03c78852982006c-37677109-2073600-17fabdc3343143f%22%7D'
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] {}
},
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}
# 1.3 ctx.request.body
需要借助中间件,解析请求体中对的body参数,挂载到ctx.request.body
const KoaBody = require('koa-body')
app.use(KoaBody());