What is the difference between node.js get and post?
Difference: 1. The parameters of the GET request are passed through the URL, while the parameters of the POST request are passed through the HTTP uplink message; 2. The security of the POST request is higher than that of the GET request, and the parameters of the GET request are visible in the URL. , so the GET request is unsafe; 3. GET has a request cache, but POST does not; 4. GET is used to retrieve data, while POST is used to submit data; 5. The amount of data transmitted by GET is limited, but the amount of data transmitted by POST There are no restrictions; 6. GET requests have restrictions on data types, while POST requests have no restrictions.
The operating environment of this tutorial: Windows 7 system, nodejs version 16, DELL G3 computer.
Node is a javaScript language running on the server side. Users need to use the get/post method to send requests to the server.
The functions implemented by get and post are basically the same. The client submits data to the server, but the implementation mechanism is different.
GET request
The GET request adds the parameter data queue to the url pointed to by the action attribute of the form. The value is the same as in the form. The name attribute corresponds one to one and can be seen in the URL. The req.url sent is processed through parse() in the url module.
We send the get request through the form form:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>get</title> </head> <body> <form action="http://localhost:8080/index" method="get"> 用户:<label> <input type="text" name="user" value=""> </label><br> 密码:<label> <input type="password" name="pass" value=""> </label><br> <input type="submit" value="提交"> </form> </body> </html>
The corresponding node.js code is as follows:
const http=require('http'); const urlLib=require('url'); http.createServer(function (req, res){ //req获取前台请求数据 //req.url的值是:/index?user=Kity&pass=32412 var obj=urlLib.parse(req.url, true); var url=obj.pathname;//url的值是:"/index" var GET=obj.query; //GET的值是:{user:'Kity',pass:'232312'} console.log(url, GET); res.write('success'); res.end(); }).listen(8080);
The result after running the node.js code is as follows:
liyabin@liyabin-ThinkPad-Edge-E430:~/下载/node$ node server3.js /index { user: 'Kity', pass: '231312' }
POST request
All the contents of the POST request are in the request body, and all node.js will not parse the request body by default. Post requests are processed through parse() in the querystring module. The amount of data transmitted by post is much larger than that of get. It will not be transmitted at once and needs to be arrived in segments.
To send a post request form, just change the above method="get" to method="post".
const http=require('http'); const querystring=require('querystring'); http.createServer(function (req, res){ //POST——req var str = ''; //接收数据 //data——有一段数据到达(很多次) req.on('data', function (data){ let i = 0; console.log(`第${i++}次收到数据`); str += data; }); //end——数据全部到达(一次) req.on('end', function (){ let POST = querystring.parse(str); console.log(POST); res.write("success"); res.end(); }); }).listen(8080);
The result after running the node code is as follows:
liyabin@liyabin-ThinkPad-Edge-E430:~/下载/node$ node server.js 第0次收到数据 { user: 'fdf', pass: '21341412' }
The difference between get and post
The advantages and disadvantages of GET and POST requests:
(1) The amount of data transmitted by get is very small, generally around 2k, but the execution efficiency is better than post;
(2) The amount of data transmitted by post is large. It is waiting for the server to read the data, but there is also a byte limit. This is to prevent attacks on the server with large amounts of data. Microsoft uses Request.Form( ) There is a limit to the maximum data received, IIS4 is 80kB, IIS5 is 100kB;
(3) Form submission generally uses post, because if you use get to submit data, the user name and password will appear on the url. If the page It can be cached or other users can access the client, and the user name and password can be seen from the history record, which brings data security issues.
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of What is the difference between node.js get and post?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
