The following editor will share with you an article based on routing rules and methods of obtaining request parameters in express. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.
Common routing rules in express
The main routing rules used are get and post. That is,
var express = require('express'); var app = express(); app.get(); // get和post两种请求方式 app.post();
The first parameter of app.get() and app.post() is the request path, and the second parameter is the callback function for processing the request. ;The callback function has two parameters, req and res, which represent request information and response information.
Get the request path and various parameters in the request body
The path request and the corresponding form of getting the request path are as follows:
(1) req.query (query the parameters in the get request)
GET /shoes?order=desc&shoe[type]=converse&shoe[color]=blue req.query.order // =>'desc' req,query.shoe.type // =>'converse'
(2) req.body (query request body)
// POST user[name]=dby&user[email]=bing@163.com req.body.user.name // =>'dby'
(3) req.params
// GET /file/javascript/jquery.js req.params[0] // => 'javascript/jquery.js'
(4) req.params(name)
// ?name=tobi req.params(name) // => 'tobi' // POST name=tobi req.param('name') // => 'tobi'
The meaning of various acquisition paths can be clearly seen from the above code:
req.query: Process the get request and obtain the request parameters of the get request
req.params: Process the get or post request in the form of /:xxx and obtain the request parameters
req.body: Process the post request and obtain the request body of the posted request
req.param(): handles get and post requests, but the search priority from high to low is req.params->req.body->req.query
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to package js with webpack
Vue Example of a simple auto-complete input box
angular5 httpclient example practice
The above is the detailed content of Methods based on routing rules and obtaining request parameters in express. For more information, please follow other related articles on the PHP Chinese website!