当前位置:网站首页>Nodejs get get/post request parameters
Nodejs get get/post request parameters
2022-06-26 12:27:00 【C+ Ankou wood】
One 、 Native node How to get get/post Request parameters
Handle get Request parameters , Use node Built in core module ----url modular
url.parse(); Method will be a complete URL Address , Divided into many parts , Commonly used :host、port、pathname、path、query.
The first parameter is the address ,
The second parameter is... By default false, Set to ture after , Convert string format to object format . character string (“a=1&b=2”) Convert to object format ({a: 1,b: 2}).
var url = require("url");
//req.url='/users?a=1&b=2'
var urlObj = url.parse("/users?a=1&b=2" ,true);
console.log(urlObj );//{a:1,b:2}
Handle post Request parameters
To get post Parameter object , Third party packages are required querystring
req.body = querystring.parse(str); // querystring.parse Is to convert a string into an object {a:1,b:2}
Two 、express Get the parameter method of the request in the framework
obtain get Requested parameter value (req.query)
from nodejs Provided by default , No need to load middleware ,
This method is mostly applicable to GET request , analysis GET Parameters in the request
The object that contains the properties of each query string parameter in the route , If not, it is {}
// visit 127.0.0.1/user?name=zd&age=18
console.log(req.query);//{name:zs,age:18}
obtain url route (req.params)
nodejs Provided by default , There is no need to load other middleware
req.params Contains routing parameters ( stay URL The path part of ), and req.query contain URL The query parameters of ( stay URL Of ? Later parameters ).
app.get("/user/:id", function (req, res) {
// visit 127.0.0.1/user/3
console.log(req.params);//{id:3}
});
obtain post Requested parameter value (req.body)
req.body No nodejs Provided by default , Middleware needs to be loaded body-parser Middleware can be used req.body.
express Integrated body-parser, You can introduce , If not, please download and import by yourself
demo:
const express = require("express");
const app = express();
//const crypto = require("crypto");// encryption
//const hash = crypto.createHash("md5");
const bodyParser = require("body-parser");//body Argument parsing
app.use(bodyParser.urlencoded({
extended: false })); //parse application/x-www-form-urlencoded
app.use(bodyParser.json()); //parse application/json
// app.use(bodyParser()); // This method is obsolete , Replaced by the above two
app.get("/", function (req, res) {
console.log(req.query);
res.send("helloworld");
});
app.get("/user/:id", function (req, res) {
console.log(req.params);//{id:xx}
res.send("helloworld");
});
app.post("/user", function (req, res) {
console.log(req.body);
// Processing logic ... Inquire about sql
res.send({
name: "yang", age: 18 });
});
app.listen(80, function () {
console.log("express is running");
});
In general , The following important modules are needed with express The frame is installed together :
body-parser - node.js middleware , Used for processing JSON, Raw, Text and URL Encoded data .
cookie-parser - This is an analysis Cookie Tools for . adopt req.cookies You can take it from cookie, And turn them into objects .
multer - node.js middleware , Used for processing enctype=“multipart/form-data”( Set the MIME code ) Form data for .
边栏推荐
- Analysis report on China's photovoltaic inverter market prospect forecast and investment strategy recommendations in 2022
- Investment planning and forecast report on the future direction of China's smart agriculture during the 14th five year plan (2022)
- 2021 q3-q4 investigation report on the use status of kotlin multiplatform
- Basic principle of MOS tube and important knowledge points of single chip microcomputer
- How long ago did PHP get
- Spark-day02-core programming-rdd
- Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding
- 7-1 数的范围
- Which is safer and better for great wisdom to open an account
- Ad - update the modified PCB package to the current PCB
猜你喜欢
![[graduation season · advanced technology Er] I remember the year after graduation](/img/e7/8e1dafa561217b77a3e3992977a8ec.png)
[graduation season · advanced technology Er] I remember the year after graduation

"Pinduoduo and short video speed version", how can I roast!

ctfshow web入门 命令执行web75-77

2021 q3-q4 investigation report on the use status of kotlin multiplatform

The laravel dingo API returns a custom error message

International beauty industry giants bet on China

MS17_ 010 utilization summary

MOS管基本原理,单片机重要知识点

New routing file in laravel framework

HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 3 freertos-mpu protection bypass
随机推荐
NFS shared storage service installation
PHP generate order number
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
Leetcode 78. 子集 and 90. 子集 II
这两天搭建环境遇到的几个问题
Five strategies and suggestions of member marketing in consumer goods industry
2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
Omnichannel membership - tmall membership 1: opening tutorial
redis通过6379端口无法连接服务器
Five trends of member marketing of consumer goods enterprises in the future
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
CG bone animation
房租是由什么决定的
Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding
我想知道同花顺是炒股的么?在线开户安全么?
Hello! Forward proxy!
How do consumer goods enterprises formulate membership interests?
4. N queen problem
Is it safe to open a securities account in general
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构