访问静态资源
静态资源都放在public里面
我们在public/images里面放一张图片odst.jpg
就可以在浏览器通过http://localhost:3000/images/odst.jpg访问到这章图片
获取get参数
// public/sports.html
var xhr = new XMLHttpRequest()
xhr.onload = () => {
console.log('response arrived')
}
// get请求参数放在url?后面,通过查询字符串参数形式发送
xhr.open('get','/list?type=login&vip=false&level=1')
xhr.send()
console.log('xxxxx')
// router/index.js
const router = require('koa-router')()
router.get('/list', async (ctx, next) => {
console.log(ctx.request.query)
ctx.body = 'list data'
})
通过ctx.request.query
获取get参数
获取post参数
post请求默认通过纯文本的方式把请求体内容传给后端,通过send()方法参数进行传输
post请求可以把"key/value",文件,Json类型的数据通过请求体传给后端,需要通过设置setRequestHeader()
方法进行设置
// public/sports.html
var xhr = new XMLHttpRequest()
xhr.onload = () => {
console.log('response arrived')
}
xhr.open('post', '/list2')
// setRequestHeader必须写在open()和send()中间才生效
// 向服务端发送什么格式的数据就配置什么类型的Content-Type
xhr.setRequestHeader('Content-Type', 'application/x-www-