To send API requests to a different Docker container using NextJS and NodeJS, you can follow the steps below:
axios
package using npm install axios
.axios
package and create an instance of it, ensuring that the baseURL
of the instance is set to the IP address of the container that you want to send requests to.axios
instance to send requests to the desired endpoint on the other container.Here is some code that demonstrates how to make an API request to a container running a simple NodeJS server:
import axios from 'axios';
// Create an instance of axios with the baseURL of the container you want to send requests to
const apiClient = axios.create({
baseURL: 'http://<ip_address_of_container>:<port_number>'
});
// Make a GET request to the desired endpoint on the other container
apiClient.get('/api/data').then(response => {
console.log(response.data); // The response from the other container's endpoint
}).catch(error => {
console.error(error);
});
You can customize this code to suit your specific needs, depending on the types of requests you need to make and the data you expect to receive in response.
Asked: 2022-07-14 11:00:00 +0000
Seen: 12 times
Last updated: Nov 19 '21