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.');
});
}
Lord Lokhraed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.