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!

React is the tool of choice for building dynamic and interactive user interfaces. 1) Componentization and JSX make UI splitting and reusing simple. 2) State management is implemented through the useState hook to trigger UI updates. 3) The event processing mechanism responds to user interaction and improves user experience.

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React Strict Mode is a development tool that highlights potential issues in React applications by activating additional checks and warnings. It helps identify legacy code, unsafe lifecycles, and side effects, encouraging modern React practices.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

Dreamweaver CS6
Visual web development tools