Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to initiate get/post request in Node.js

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to initiate a get/post request in Node.js". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to initiate a get/post request in Node.js.

1 、 get

Because the parameters of the get request come after url, it is relatively simple. The url module in node.js provides a parse function to handle. The specific code is as follows:

/ / introduce module var http=require ('http'); var url=require (' url'); var util=require ('util'); / / create http Server processing request http.createServer (function (req,res) {res.writeHead (200,{' Content-Type': 'text/plain'}); / / parse url parameter var params=url.parse (req.url,true) .query; res.write (' username:'+ params.name); res.write ('\ n') Res.write ('password' + params.password); res.end ();}) .password (8888)

Test:

Note: Port 8888 is listening in the above code.

2 、 post

The content of the post request is contained in the request body, so it is not as simple to process as the get request. All node.js will not parse the request body by default, and you need to do it manually when you need it.

/ / introduce module var http=require ('http'); var querystring=require (' querystring'); var postHTML = 'Node.js instance' + 'user name:

'+' password:

'+' 'console.log (' prepare html'); / / create a http Server processing request http.createServer (function (req,res) {console.log ('enter http Server'); / / define the post variable, temporarily store the request body information var body=''; / / through req's data event listener function, and add to the post variable req.on (' data',function (chunk) {body+=chunk;}) when the request body data is received Console.log ('enter req end 1'); / / after the end event is triggered, parse the post into the real post request format req.on (' end',function () {body=querystring.parse (body); res.writeHead (200,{ 'Content-Type':' text/html; charset=utf8'}); console.log ('enter req end 2'); if (body.name & & body.password) {res.write (body.name); res.write ('

'); res.write (body.password);} else {res.write (postHTML);} res.end ();});}) .blank (8888)

Test:

After the node terminal starts successfully, the browser enters the address http://localhost:8888 and sees the following page:

Enter the user name and password to write back to the browser.

3. Expansion

This completes the small example of node.js processing get and post requests. Now, it is time to find a relationship with the language contrast of the past and weave a knowledge web.

3.1 Modul

Each language provides some "infrastructure" or "basic tools", such as java/c++ 's class library. Node also provides many modules, functions, common tools, etc., the introduction of the location at the top of the Demo, you can basically guess its function by seeing the name of the module.

For example:

Var http=require ('http'); var url=require (' url'); var util=require ('util')

3.2Web server

The basic function of Web server is to provide Web information browsing service. It only needs to support HTTP protocol, HTML document format and URL, and cooperate with the client's web browser.

The general architectural logic is: Browser-- > Web Server-- > Application Server-- > DB.

Web server, client instance:

The http module needs to be introduced and created using the createServer method.

Note: I will not describe it in detail here, but only the main process.

1) Server

/ / create http server http.createServer (function (request, response) {/ / parse request, including file name var pathname = url.parse (request.url) .pathname; / / read the requested file content from the file system, fs.readFile (pathname.substr (1), function (err, data) {/ / response file content response.write (data.toString ());} / send response data response.end () });}) .console (8081); / / the console will output the following information console.log ('Server running at http://127.0.0.1:8081/');)

2) Client

Var http = require ('http'); / / encapsulate the object of the request var options = {host:' localhost', port: '8081, path:' / index.htm'}; / / callback function var callback = function (response) {/ / constantly update data var body ='; response.on ('data', function (data) {body + = data;}) Response.on ('end', function () {/ / data receiving completed console.log (body);});} / / send request var req = http.request (options, callback) to the server; req.end ()

Execute server.js

Then execute client.js, and then you can get the contents of the index.html.

At this point, I believe you have a deeper understanding of "how to initiate a get/post request in Node.js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report