This article will introduce a Node. js to handle GET/POST requests. I hope this method will help you all.
We need to view the HTTP request,
Extracts the request URL and GET/POST parameters from it,
Then "Route" runs the corresponding code based on the data.
To parse the data, we need an additional Node. js module,
They are url and querystring respectively. For details about how to use them, click the link to view the corresponding manual or refer to the following table:
The Code is as follows: |
Copy code |
Url. parse (string). query | Url. parse (string). pathname | | | ------------------------- Http: // localhost: 8888/start? Foo = bar & hello = world -------- | | Querystring. parse (string) ["foo"] | | Querystring (string). parse ["hello"]
|
Now that we know how Node. js processes GET/POST requests, let's write a simple GET request processing:
The Code is as follows: |
Copy code |
Var http = require ('http '); Var url = require ('url '); Http. createServer (function (req, res ){ // Obtain the URL path and print it on the console Var pathname = url. parse (req. url). pathname; Console. log ('request for '+ pathname + 'stored ed .'); Res. writeHead (200, {'content-type': 'text/plain '}); Res. end ('Hello worldn '); }). Listen (1337, '192. 0.0.1 '); Console. log ('server running at http: // 127.0.0.1: 1337 /');
|
At this point, we can use the URL path to differentiate different GET/POST requests,
Compared with GET requests, POST requests are generally "heavy". To make the entire process non-blocking,
Node. js splits the POST data into many small data blocks, and then sends these small data blocks to the callback function by triggering specific events.
Specific events include data (new small data block arrival) and end (all data has been received ),
You can click the link to view the corresponding manual or refer to the following code:
The Code is as follows: |
Copy code |
Var http = require ('http '); Http. createServer (function (req, res ){ Var postData = ''; // Set the received data encoding format to UTF-8 Req. setEncoding ('utf8 '); // Receive data blocks and assign them to postData Req. addListener ('data', function (postDataChunk ){ PostData + = postDataChunk; }); Req. addListener ('end', function (){ // After receiving the data, run the callback function. }); Res. writeHead (200, {'content-type': 'text/plain '}); Res. end ('Hello worldn '); }). Listen (1337, '192. 0.0.1 '); Console. log ('server running at http: // 127.0.0.1: 1337 /'); |
Post request with Parameters
The Code is as follows: |
Copy code |
Var http = require ('http '); Var querystring = require ('querystring '); Var post_data = querystring. stringify ({ Product: 'Club ', Sign: 'ddddddddddddddd ', Sender: 'name of the sender: Super admin ', Uids: ['ffwq @ qq.com ', 'ffqwf @ www.com'], Msg: 'www' });
Var options = { Host: '10. 11.442.33 ', Port: 80, Path: '/ww1 ', Method: 'post' }; Var req = http. request (options, function (res ){ Console. log ('status: '+ res. statusCode ); Console. log ('headers: '+ JSON. stringify (res. HEADERS )); Res. setEncoding ('utf8 '); Res. on ('data', function (chunk ){ Console. log ('body: '+ chunk ); }); });
// Write data to request body Req. write (post_data + "n "); Req. end (); |