Home > Web Front-end > JS Tutorial > Methods based on routing rules and obtaining request parameters in express

Methods based on routing rules and obtaining request parameters in express

亚连
Release: 2018-05-31 09:57:23
Original
1779 people have browsed it

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();
Copy after login

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'
Copy after login

(2) req.body (query request body)

// POST user[name]=dby&user[email]=bing@163.com
req.body.user.name
// =>'dby'
Copy after login

(3) req.params

// GET /file/javascript/jquery.js
req.params[0]
// => 'javascript/jquery.js'
Copy after login

(4) req.params(name)

// ?name=tobi
req.params(name)
// => 'tobi'
// POST name=tobi
req.param('name')
// => 'tobi'
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template