当前位置:网站首页>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 .

原网站

版权声明
本文为[C+ Ankou wood]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170516079761.html