Koa params: Introduction to parameter configuration in the koa definition interface, paramskoa

Source: Internet
Author: User

Koa params: Introduction to parameter configuration in the koa definition interface, paramskoa

We will use the interfaces provided by the backend in front-end development,

For example:

http://www.x.com/addUser?name=Arvo&age=24

In express or koa, how can we implement such an interface with parameters,

See the following code:

Var router = require ('koa-router '); var querystring = require ('querystring'); app. use (router (app); app. get ('/adduser', function * (next) {if (! This. req. _ parsedUrl. query) {this. body = "parameter error"; return;} var params = querystring. parse (this. req. _ parsedUrl. query );...});

Note:

1. access through the browser (set the port or something, I will start 3000 here)

localhost:3000/addUser?name=Arvo&age=24

2. In this way, the parameters can be obtained based on our route configuration.

App. get ('/adduser', function * (next) {if (! This. req. _ parsedUrl. query) {this. body = "parameter error"; return;} var params = querystring. parse (this. req. _ parsedUrl. query); console. log (params. name) console. log (params. age )});


3. The querystring module comes with node. 

4. Check the req and other parameters on your console or official website.


Hope to help me with the same small white.



Github: github.com/ArvoGuo


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.