Cannot POST /api/signup in Express.js application despite route configuration

Problem:
I am building an Express.js application with a /signup endpoint under the /api route. However, when I try to POST to /api/signup, I receive a Cannot POST /api/signup error in response.

I have double-checked my code, and the route seems to be configured correctly. The server starts without any errors, and I can see the log confirming that the server is running on the expected port. I also verified that MongoDB is running and accessible.

What I Have Tried:
Verified that the route exists in my authRoutes file and that it is correctly imported and mounted in the app.js file.
Checked the server logs and confirmed there are no runtime errors.
Ensured that MongoDB is connected and the database is properly set up.
Tried making the POST request with curl and a REST client like Postman, but the result is always Cannot POST /api/signup.
Code:

server.js:

const app = require('./src/app');
const dotenv = require('dotenv');
    
dotenv.config();
    
const PORT = process.env.PORT || 5000;
    
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

src/app.js:

const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const authRoutes = require('./routes/auth');
    
dotenv.config();
    
const app = express();
    
// Middleware
app.use(express.json());
    
// MongoDB Connection
mongoose
  .connect(process.env.MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("Connected to MongoDB"))
  .catch((err) => console.error("MongoDB connection error:", err));

    
// Routes
app.use('/api', authRoutes);
    
module.exports = app;

src/routes/auth.js:

const express = require('express');
const router = express.Router();
const { signup } = require('../controllers/authController');
    
router.post('/signup', signup);
    
module.exports = router;

src/controllers/authController.js:

const bcrypt = require('bcrypt');
const User = require('../models/User');
    
exports.signup = async (req, res) => {
  try {
    const { email, password } = req.body;
    const hashedPassword = await bcrypt.hash(password, 10);
    const user = new User({ email, password: hashedPassword });
    await user.save();
    res.status(201).json({ message: 'User created successfully' });
  } catch (err) {
    console.error(err);
    res.status(500).json({ message: 'An error occurred' });
  }
};

MongoDB Connection:
The connection string is defined in my .env file:

MONGO_URI=mongodb://localhost:27017/devtrust
JWT_SECRET=your-local-jwt-secret
PORT=5000

Testing:
I use curl to test the endpoint:

curl -X POST http://localhost:5000/api/signup 
-H "Content-Type: application/json" 
-d '{"email": "[email protected]", "password": "securepassword"}'

The response I get is:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Error</title>
  </head>
  <body>
    <pre>Cannot POST /api/signup</pre>
  </body>
</html>

What I Expect:
I expect the server to hash the password, store the user in MongoDB, and respond with a 201 Created status.

Environment:
Node.js: v18.20.5
MongoDB: 6.0
Express.js: 4.18.2

What I Need Help With:

Why am I receiving the Cannot POST /api/signup error despite having the route defined?

Are there any issues with how I have configured the routes or middleware?

Is there anything I may be overlooking in the setup?

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