Docker compose including node js and html

Iam Running a docker compose that has html and mysql and nodejs
mapping mysql 3306:3306 and nodejs 3000:3000
Iam 100% that they are connected throught the same network on the docker compose

When I run the docker-compose up it says that nodejs connected to mysql database and listenning on port 3000
but from my html when i try to fetch it gave me error fetching
Literally tried everything but it’s refusing to fetch

here is the error i get when i try to fetch

User
nodejs:3000/addStudent:1 
        
        
        Failed to load resource: net::ERR_NAME_NOT_RESOLVED
Team.html:114  Error: TypeError: Failed to fetch
    at addStudent (Team.html:102:5)
    at HTMLInputElement.onclick (Team.html:1:1)  

here is my nodejs `

const express = require('express');
const mysql = require('mysql2');
const cors = require('cors');

const app = express();
const port = 3000;

app.use(cors({
  origin: 'http://localhost:8080', // Replace with your frontend origin
  methods: ['POST'], // Allow only POST requests
  allowedHeaders: ['Content-Type'], // Allow only Content-Type header
}));


const connection = mysql.createConnection({
  host: 'mysql',
  user: 'abdullah',
  password: '123564',
  database: 'maybe',
});
connection.connect((err) => {
  if (err) {
    console.error('Error connecting to MySQL:', err);
    process.exit(1); // Exit the application if unable to connect to MySQL
  }
  console.log('Connected to MySQL database!');
});

app.use(express.json());


app.post('/addStudent', (req, res) => {
  console.log('Request body:', req.body);

  const { name, age, cgpa, student_id } = req.body;

  const query = 'INSERT INTO students (name, age, cgpa, student_id) VALUES (?, ?, ?, ?)';
  connection.query(query, [name, age, cgpa, student_id], (err, results) => {
    if (err) {
      console.error('Error inserting student into database:', err);
      res.status(500).json({ message: 'Error inserting student into database' });
      return;
    }
    console.log(`Inserted student with ID: ${results.insertId}`);
    res.json({ message: 'Student added successfully' });
  });
});

app.listen(port, () => {
  console.log(`Server is listening on port ${port}`);
});

and here is my docker-composer yml

version: '3.7'

services:
  mysql:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: "mainpassword"
      MYSQL_DATABASE: "maybe"
      MYSQL_USER: "abdullah"
      MYSQL_PASSWORD: "123564"
    ports:
      - "3306:3306"
    volumes:
      - ./mysql_data:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
      
    networks:
      - internalnet

  nginx:
    build:
      context: .
      dockerfile: Dockerfile.nginx
    ports:
      - "8080:80"
    depends_on:
      - mysql # Assuming your Node.js service depends on a MySQL service
    volumes:
      - ./site-content/html:/usr/share/nginx/html
      - ./site-content/css:/usr/share/nginx/html/css
      - ./site-content/webfonts:/usr/share/nginx/html/fonts
      - ./site-content/images:/usr/share/nginx/html/images
    
    networks:
      - internalnet

  nodejs:
    build:
      context: .
      dockerfile: Dockerfile.nodejs
    ports:
      - "3000:3000"
    expose:
      - 3000
    depends_on:
      - mysql # Assuming your Node.js service depends on a MySQL service
    networks:
      - internalnet

networks:
  internalnet:
    driver: bridge

and this is the fetching method from my html script iam trying to run in the html

   fetch('http://nodejs:3000/addStudent', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ name, age, cgpa, id })
    })
    .then(response => response.json())
    .then(data => {
        alert(data.message);
    })
    .catch(error => {
        console.error('Error:', error);
        alert('An error occurred. Please try again.');
    });
}

New contributor

Lord Lokhraed 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