I am trying to have a NodeJS server respond with data from an external API. Consider the index.js file:
const express = require('express'); const axios = require('axios'); const app = express(); app.get('/api', async function (req, res) { url = 'http://freegeoip.app/json/'; // external API request var ax = axios.get(url) .then(function(data) { res.json(data.data) }) .catch(function(err) { res.status(404); }) }) app.listen(3000); When I visit http://localhost:3000/api the JSON data from http://freegeoip.app/json/ is presented and loads within 500 ms.
The problem is my NodeJS server in production runs within a Docker container. Running the exact same code in my Docker container takes approximately 5 seconds to load. I suspect this is a DNS resolution issue from the Docker container. Is there a way to show time spent on an external API request with axios? If this is a DNS issue, it seems to only affect NodeJS as I can curl from another Docker container.
For reference below is the Dockerfile and docker-compose.yml:
FROM node:15.14.0-alpine3.13 WORKDIR /backend RUN npm install express RUN npm install axios RUN npm install -g nodemon COPY . . CMD ["nodemon", "/backend/index.js"] version: '3' services: backend: restart: always build: ./backend volumes: - ./backend:/backend - /backend/node_modules ports: - "3000:3000"