Ask Your Question
3

How to send API requests to a different docker container, using NextJS and NodeJS?

asked 2022-07-14 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-11-19 16:00:00 +0000

huitzilopochtli gravatar image

To send API requests to a different Docker container using NextJS and NodeJS, you can follow the steps below:

  1. First, ensure that both containers are running on the same Docker network.
  2. In the container that will be making the API requests, install the axios package using npm install axios.
  3. Import the 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.
  4. Use the 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-14 11:00:00 +0000

Seen: 12 times

Last updated: Nov 19 '21