Home  >  Article  >  Web Front-end  >  nodejs request server data type

nodejs request server data type

PHPz
PHPzOriginal
2023-04-17 15:03:1999browse

As the separation of front-end and back-end becomes more and more mainstream, Node.js, as a very powerful back-end technology, is increasingly favored by many developers. Requesting server data in Node.js is also a very important part. This article will briefly introduce the common request server data types in Node.js.

1. GET request

GET request is a common server request method. You can use GET request to obtain resources on the server. Initiating a GET request in Node.js is very simple. You only need to call the get method of the http module. The sample code is as follows:

<code>const http = require('http');

http.get('http://www.example.com', (res) => {
  console.log('Got response: ' + res.statusCode);
  res.on('data', (chunk) => {
    console.log('BODY: ' + chunk);
  });
}).on('error', (e) => {
  console.log('Got error: ' + e.message);
});</code>

In the above code, we use the get method of the http module to initiate a request to the specified server. Make a GET request and receive the data returned by the server after the request is successful. When the request is successful, we print out the status code of the server response and receive the data returned by the server through the res.on method.

2. POST request

POST request is a common method of data submission. It is also very simple to initiate a POST request in Node.js. We can use the request method of the http module to initiate a POST request. The sample code is as follows:

<code>const http = require('http');

// post option
const options = {
  hostname: 'www.example.com',
  path: '/postdata',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
};

// create request
const postReq = http.request(options, (res) => {
  console.log('status code: ' + res.statusCode);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log('body: ' + chunk);
  });
});

// post data
postReq.write('key1=value1&key2=value2');
postReq.end();</code>

In the above code, we first define the options parameters of a POST request, including the requested url, request method and request First class. After that, we created a POST request using the http.request method, and wrote the POST data to be sent through the postReq.write method. Finally, we end the request and submit the POST data through the postReq.end method.

3. JSON data request

In modern Web development, JSON data request has gradually become a common data request method. Initiating a JSON data request in Node.js is also very simple. We can use the request method in the http module and set the Content-Type to 'application/json'. The sample code is as follows:

<code>const http = require('http');

// json request options
const options = {
  hostname: 'www.example.com',
  path: '/postjson',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
};

// create request
const postReq = http.request(options, (res) => {
  console.log('status code: ' + res.statusCode);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log('body: ' + chunk);
  });
});

const postData = {
  'key1': 'value1',
  'key2': 'value2'
};

// post json data
postReq.write(JSON.stringify(postData));
postReq.end();</code>

In the above code , we also use the request method in the http module to create a POST request, and set the Content-Type to 'application/json', and then convert the JSON data to be sent into a string through the JSON.stringify method and pass it through postReq. The write method writes the request. Finally, we also end the request and submit the data through the postReq.end method.

Summary:

This article briefly introduces the three common request server data types in Node.js, including GET requests, POST requests and JSON data requests. In actual development, we need to choose the most appropriate data request method according to the actual situation, and configure the request parameters according to the specific application scenarios in order to achieve more flexible and efficient data requests.

The above is the detailed content of nodejs request server data type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn