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!