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

May 31, 2018 am 09:57 AM
express Obtain rule

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get file extension in Python? How to get file extension in Python? Sep 08, 2023 pm 01:53 PM

How to get file extension in Python?

Use math.Max ​​function to get the maximum value in a set of numbers Use math.Max ​​function to get the maximum value in a set of numbers Jul 24, 2023 pm 01:24 PM

Use math.Max ​​function to get the maximum value in a set of numbers

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to use express to handle file upload in node project

How to get the last element of LinkedHashSet in Java? How to get the last element of LinkedHashSet in Java? Aug 27, 2023 pm 08:45 PM

How to get the last element of LinkedHashSet in Java?

Java program to get the size of a given file in bytes, kilobytes and megabytes Java program to get the size of a given file in bytes, kilobytes and megabytes Sep 06, 2023 am 10:13 AM

Java program to get the size of a given file in bytes, kilobytes and megabytes

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

Get the latest updates now: Fix missing latest updates Get the latest updates now: Fix missing latest updates Nov 08, 2023 pm 02:25 PM

Get the latest updates now: Fix missing latest updates

See all articles