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