koa2 gets HTTP request parameters

HTTP

What is HTTP

HTTP, Hypertext Transfer Protocol, is a response protocol that implements communication between a client and a server, and it is used as a request between a client and a server.

The client (browser) submits an HTTP request to the server; the server then returns a response to the client; the response contains status information about the request and may also contain the content of the request.

HTTP request method

Eight methods (sometimes called "actions") are defined in the HTTP/1.1 protocol to indicate different operation modes of resources specified by Request-URL

Among them:
HTTP1.0 defines three request methods: GET , POST and HEAD methods.
HTTP1.1 adds five new request methods: OPTIONS , PUT , DELETE , TRACE and CONNECT methods

The four most commonly used request methods: GET , POST , PUT , DELETE .

Get HTTP request parameters

Official website address: https://www.koajs.com.cn/#context

For more exciting content, please search for " " on WeChat前端爱好者 , and click me to view .

get get request

Choose one of the following two ways

let { id } = ctx.request.query 
let { id } = ctx.query

example


const router = new Router({
    prefix: '/user'
}) 

router.get('/del', async (ctx) => {
   let { id } = ctx.request.query 
    console.log(id)
    ctx.body = "删除用户"
})

send request

result

Example 2


const router = new Router({
    prefix: '/user'
}) 

router.get('/del', async (ctx) => {
    let { id } = ctx.query
    console.log(id)
    ctx.body = "删除用户"
})

send request

result

get post request

Use ctx.request.body (this method is wrong)

router.post('/add', async (ctx) => {
	let {username,pwd} = ctx.request.body
	console.log(username,pwd)
	ctx.body = '添加用户'
})

send request

Note: It is obvious that a 500 error is reported here. Check the console and the error is as follows:

console display

Reason : ctx.request.body , this one doesn't exist.

Solution : Get the post request parameters. It is cumbersome to use native ones and needs to be converted. Use the middleware [ koa-bodyparser ] to get the post request parameters.

Use the middleware koa-bodyparser

Install middleware koa-bodyparser

npm install koa-bodyparser --save

Introduce the use of middleware

const bodyParser = require('koa-bodyparser')
app.use(bodyParser())

send request

result

Get routing parameters

let id = ctx.params.id

example

router.get('/find/:id', async (ctx) => {
	let id = ctx.params.id
	console.log(id)
	ctx.body = '这是用户首页'
}

send request

result

full code

const koa = require('koa')
const bodyparser = require('koa-bodyparser')
const Router = require('koa-router')
const app = new koa()
const router = Router({
	prefix: '/user'
})
 
app.use(bodyparser())
app.use(router.routes())

router.get('/', async (ctx) => { 
	ctx.set("Allow","GET,POST")
	ctx.status = 301
	ctx.body = {
		code: 200,
		msg: '这是请求首页的信息'
	}
})

//  获取路由参数
router.get('/find/:id', async (ctx) => {
	let id = ctx.params.id
	console.log(id)
	ctx.body = '这是用户首页'

})

// 获取get请求参数
router.get('/del', async (ctx) => {
	let {id} = ctx.request.query 
	console.log(id)
	ctx.body = '删除用户'
})

router.get('/del2', async (ctx) => { 
	let {id} = ctx.query
	console.log(id)
	ctx.body = '删除用户'
})

// 获取post请求参数
router.post('/add', async (ctx) => {
	let {username,pwd} = ctx.request.body
	console.log(username,pwd)
	ctx.body = '添加用户'
})

app.listen(3000)

Summarize

  • Get get request parameters: ctx.request.queryorctx.query
  • Get post request parameters: use middleware [ koa-bodyparser ]
  • Get routing parameters: use ctx.params.xx to represent the required parameters

Guess you like

Origin blog.csdn.net/BradenHan/article/details/130878033