Express.js Route Conflict Causing ‘Internet Disconnected’ Error and 404 Not Found on Signup Route

I am trying to connect my site to a database. I am using Node.js, Express.js and Mongodb. I have two forms: a donation form and a signup form. Both use different schemas and routes. The donation form works fine, but when I include the signup route, I encounter issues.

So When the signup route is included in the server file:
The donation form submission results in an “Internet Disconnected” error.
The signup form submission results in a 404 Not Found error.

However When the signup route is commented out,the donation form works perfectly.

Why does the donation form work only when the signup route is commented out?And
how can I resolve the 404 error for the signup form and ensure both forms work correctly?

Here is the link to my repository

THE USER ROUTE

const express = require('express');
const User = require('../models/User');

const router = express.Router();

router.post('/signup', async (req, res) => {
    try {
        const { firstName, lastName, email, password } = req.body;
        const user = new User({ firstName, lastName, email, password });

        await user.save();
        res.status(201).send({ message: "Sign Up successful" });
    } catch (error) {
        res.status(400).send({ error: "Error signing up" });
    }
});

module.exports = router;

THE SERVER

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const userRoutes = require('./routes/UserRoute'); // Ensure this path is correct

const app = express();
const port = process.env.PORT || 3000;

// Middleware
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/NGO', {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log("Connected to MongoDB");
}).catch(err => {
    console.error("Could not connect to MongoDB", err);
});

// Define Donation Schema and Model
const donationSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    email: String,
    phone: String,
    donationType: String,
    paymentMethod: String,
    amount: Number,
    date: { type: Date, default: Date.now }
});
const Donation = mongoose.model('Donation', donationSchema);

// Define Donation Route
app.post('/donate', async (req, res) => {
    try {
        const { firstName, lastName, email, amount, phone, donationType, paymentMethod } = req.body;
        const donation = new Donation({
            firstName,
            lastName,
            email,
            amount,
            phone,
            donationType,
            paymentMethod
        });

        await donation.save();
        res.status(201).send({ message: "Donation saved successfully" });
    } catch (error) {
        res.status(400).send({ error: "Error saving donation" });
    }
});

// Use User Routes
app.use('/signup', userRoutes);

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

AND THE SCRIPT FILE HANDLING INPUTS

signUpForm.addEventListener('submit', async function (event) {
        event.preventDefault();
        const userFirstName = document.getElementById('userFirstName').value;
        const userLastName = document.getElementById('userLastName').value;
        const userEmail = document.getElementById('userEmail').value;
        const password = document.getElementById('pwd').value;
        const passwordConfirm = document.getElementById('pwdConfirmation').value;
        let confirmationMessage = document.getElementById('SignUpConfirmationMessage');

        // Validate form data
        if (userFirstName && userLastName && userEmail && password === passwordConfirm) {
            try {
                const response = await fetch('http://localhost:3000/signup', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        firstName: userFirstName,
                        lastName: userLastName,
                        email: userEmail,
                        password: password
                    })
                });
                const data = await response.json();
                if (response.ok) {
                    confirmationMessage.textContent = 'Sign up successful!';
                    signUpForm.reset();
                } else {
                    confirmationMessage.textContent = 'There was an issue processing your request. Please try again';
                }
            } catch (error) {
                confirmationMessage.textContent = 'There was an issue processing your request. Please try again';
            }
        } else if (password !== passwordConfirm) {
            confirmationMessage.textContent = 'Passwords do not match';
            document.getElementById('pwd').value = '';
            document.getElementById('pwdConfirmation').value = '';
            console.log('Passwords do not match');
        } else {
            console.log('Please fill in all details correctly');
        }
    });
}

New contributor

Kama 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