directory

What JS libraries can we use when we need to access the interfaces of other services?

Look at the documentation and go straight to Axios Github

Write Axios HelloWorld

Create a new NodeJS project and introduce the AXIos package:

The commodity service needs to be started before the request can be sent

Send a GET request

Send a POST request to simulate adding commodity data


In the process of developing back-end services, we have to invoke external service interfaces (apis of one or more services) in addition to developing the data interface of the current service.

Before I wrote a article Restify based implementation of CRUD commodity management services: blog.csdn.net/geeklevin/a… Restify helps us write service interfaces quickly.

What JS libraries can we use when we need to access the interfaces of other services?

Looking back at the Restify documentation, it has its own Restify Client component that helps developers implement calls to off-site services.

Restify.com/docs/client…

However, we will use the axios library to call external services. This library is very popular, many front-end students often use it when developing the React/Vue front-end module.

 

Look at the documentation and go straight to Axios Github

Go straight to: github.com/axios/axios

Or (Chinese friendly www.axios-js.com/zh-cn/docs/)

 

Write Axios HelloWorld

 

Create a new NodeJS project and introduce the AXIos package:

# same version as the current article

npm install [email protected]

 

The commodity service needs to be started before the request can be sent

Blog.csdn.net/geeklevin/a…

Send a GET request

const axios = require('axios');

// External interface
const api = 'http://localhost:8080/products';


// AXIos provides the corresponding HTTP request methods to send the corresponding requests such as GET, POST, and PUT
// Call the GET method on the /products interface to GET the product
axios.get(api)
  .then(function (response) {
    // Get the entire request response object here
    console.log(response);
    // Just call response.data to get commodity data
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {});Copy the code

Save the above code as getproducts.js, execute the following command, the effect is as follows:

node getProducts

By viewing the entire Response object output by console, the interface data can be obtained via Response.data, which can be further simplified by the following code:

const axios = require('axios');

// External interface
const api = 'http://localhost:8080/products';

// Write business code to process product data
const handleOnData = (data) = > {
  console.log('get data', data);
}

// Call the GET method on the /products interface to GET the product
axios
  .get(api)
  .then(function (response) {
    handleOnData(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });
Copy the code

Send a POST request to simulate adding commodity data

The other requests are similar, and this is just a quick demonstration of the use of the axios component.

Project code reference: codechina.csdn.net/geeklevin/n…