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?