nextjs unable to call API when deploy to network: [ERROR]: connect EHOSTUNREACH

I have a SSR todo app that I can run the app properly at localhost with no issue but when I deployed to the Truenas Scale, it couldn’t call the API when getting the data using getServerSideProps();

Everything is fine, and the network/port is set correctly, so the request is sent to the app server without issue.

But whatever I use Axios instance or call Axios directly in the method, the same error throws:

Error logs in truenas pod:

2024-05-01 21:38:09.420583+08:00Listening on port 50001
2024-05-01 21:39:29.909934+08:00[2024-May-01 13:39:29] - [INFO]: App init, fetch data from DB
2024-05-01 21:39:29.910995+08:00[2024-May-01 13:39:29] - [INFO]: call url: http://kenny-remote.ddns.net:50001/todo/todoList
2024-05-01 21:39:32.979784+08:00[2024-May-01 13:39:32] - [ERROR]: connect EHOSTUNREACH 58.176.231.31:50001
2024-05-01 21:39:32.980032+08:00[2024-May-01 13:39:32] - [INFO]: undefined
2024-05-01 21:39:32.982865+08:00TypeError: Cannot read properties of undefined (reading 'data')
2024-05-01 21:39:32.982898+08:00at getServerSideProps (/app/.next/server/pages/index.js:1095:41)
2024-05-01 21:39:32.982974+08:00at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-05-01 21:39:32.982997+08:00at async Object.renderToHTML (/app/node_modules/next/dist/server/render.js:508:20)
2024-05-01 21:39:32.983040+08:00at async doRender (/app/node_modules/next/dist/server/base-server.js:687:38)
2024-05-01 21:39:32.983079+08:00at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (/app/node_modules/next/dist/server/base-server.js:796:28)
2024-05-01 21:39:32.983111+08:00at async /app/node_modules/next/dist/server/response-cache/index.js:80:36

my code:

export async function getServerSideProps() {
  logger.info("App init, fetch data from DB");
  logger.info(`call url: http://${process.env.HOST_NAME}:50001/todo/todoList`)
  
  const todoList = await axios.get(`http://58.176.231.31:50001/api/todo/todoList`)
    .catch(err => logger.error("Error to init app, fetch data fail:", err)
    ) || []; // no error log was print

  // const completedList = await http.get('todo/completedList') || [];
  logger.info(todoList.data);
  return {
    props: {
      initialTodos: {
        todoList: todoList.data.data,
        // completedList: completedList.data.data,
        completedList:[]
      },
    },
  };
}

CORS config in:
next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  // reactStrictMode: true,
  swcMinify: true,
  output: 'standalone',
  async headers(){
    return [
      {
        // Routes this applies to
        source: "/api/(.*)",
        // Headers
        headers: [
          // Allow for specific domains to have access or * for all
          {
            key: "Access-Control-Allow-Origin",
            value: "*",
            // DOES NOT WORK
            // value: process.env.ALLOWED_ORIGIN,
          },
          // Allows for specific methods accepted
          {
            key: "Access-Control-Allow-Methods",
            value: "GET, POST, PUT, DELETE, OPTIONS",
          },
          // Allows for specific headers accepted (These are a few standard ones)
          {
            key: "Access-Control-Allow-Headers",
            value: "Content-Type, Authorization",
          },
        ],
      },
    ];
  }
};

module.exports = nextConfig;

It was very strange. When I call the API directly through a browser, it returns a correct result with no error!
able to call API directly

It would be great if someone could help!

tried call with Axios instance, same result

const http = axios.create({
  baseURL: process.env.HOST_NAME === undefined ? 'http://localhost:50001/api/' : `http://${process.env.HOST_NAME}:50001/api/`
});

export default http;

New contributor

Kenny Leung is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật