Koa obtains get and post parameters

As a WEB framework, the first thing we think of is how to obtain the parameters submitted by the client. In fact, in koa, we mainly divide it into get and post methods to obtain different parameters. The get method refers to the parameters behind the url, and the post is generally Refers to the parameters submitted through the form. In order to facilitate the test, we use postman to submit. In the test, we found that except get and post are different, other submission methods such as delete and put are the same as post.

const Koa = require('koa')
const app = new Koa()

app.use( async (ctx) => {
    
    
    ctx.body = {
    
    
        "url":ctx.url,
        "query":ctx.query,
        "ctx_queryString":ctx.querystring,
        body:ctx.request.body
    }
})

app.listen(3000, () => {
    
    
	console.log('start ok')
})

The output is:

{
    
    
    "url": "/?name=%E6%B8%AC%E8%A9%A6",
    "query": {
    
    
        "name": "測試"
    },
    "ctx_queryString": "name=%E6%B8%AC%E8%A9%A6",
    "body": {
    
    
        "name": "陳漮"
    }
}

From the above code, we can see that what querystring takes out is an original string, that is, what comes after the url, and what query takes out is to convert the string into an object, so that we can directly get the values ​​of its attributes , such as: ctx.query.name to get the value. In the test, it is found that the output of ctx.request.query and ctx.query are the same.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_36557877/article/details/129302972