POSTMAN CONTINOUSLY SHOWING SENDING REQUEST
postman
index.js
import express from "express";
import dotenv from "dotenv";
import { connectDB } from "./database/db.js"; // Ensure this aligns with your DB connection
import userRoutes from "./routes/user.js";
dotenv.config();
const app = express();
// Use middlewares
app.use(express.json());
// Define port
const port = process.env.PORT
// Basic route
app.get("/", (req, res) => {
res.send("Server is working");
});
app.use("/api", userRoutes);
// Add additional routes if needed
// Start the server and connect to the database
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
connectDB(); // Ensure this is correctly implemented
});
controllers user.js
import { User } from '../models/User.js';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import sendMail from '../middleware/sendMail.js';
import TryCatch from '../middleware/TryCatch.js';
// Register a new user
export const register = TryCatch(async (req, res) => {
const { email, name, password } = req.body;
// Check if the user already exists
let user = await User.findOne({ email });
if (user)
return res.status(400).json({
message: "User already exists",
});
// Hash the user's password
const hashPassword = await bcrypt.hash(password, 10);
user = {
name,
email,
password: hashPassword,
};
// Generate OTP and activation token
const otp = Math.floor(Math.random() * 1000000);
const activationToken = jwt.sign(
{
user,
otp,
},
process.env.Activation_Secret,
{
expiresIn: '5m',
}
);
const data = {
name,
otp,
};
// Send OTP email
await sendMail(email, 'E-learning', data);
res.status(200).json({
message: 'OTP sent to your email',
activationToken,
});
});
// Verify user registration
export const verifyUser = TryCatch(async (req, res) => {
const { otp, activationToken } = req.body;
// Verify activation token
const verify = jwt.verify(activationToken, process.env.Activation_Secret);
if (!verify)
return res.status(400).json({
message: 'OTP expired',
});
// Check OTP
if (verify.otp !== otp)
return res.status(400).json({
message: 'Wrong OTP',
});
// Create new user
await User.create({
name: verify.user.name,
email: verify.user.email,
password: verify.user.password,
});
res.json({
message: 'User registered',
});
});
routes user.js
import express from "express";
import { register, verifyUser } from '../controllers/user.js';
const router = express.Router();
router.post("/user/register", register);
router.post("/user/verify", verifyUser);
export default router;
db.js
import mongoose from "mongoose";
export const connectDB = async () => {
try {
await mongoose.connect(process.env.DB)
console.log("database connected");
} catch (error) {
console.log(error);
}
};
What I have tried so far:
Verified Server is Running:
The server starts and logs “Server is running on http://localhost:4000” and “database connected”.
Checked for Code Issues:
Ensured there are no syntax errors or unhandled exceptions in my code.
Increased Logging:
Added console logs in the controller to trace request handling.
Firewall and Port Issues:
Allowed Node.js through the Windows firewall.
Opened port 4000 in Windows firewall settings.
Tried disabling SSL certificate verification in Postman.
Used Different Clients:
Attempted requests via Postman, curl, and browser.
Received Cannot GET /api/user/register in the browser.
Postman and curl both resulted in ECONNRESET.
Curl Command:
Used the following curl command:
sh
Copy code
curl -X POST http://localhost:4000/api/user/register -H “Content-Type: application/json” -d “{“name”: “Anirudh”, “email”: “[email protected]”, “password”: “password”}”
Questions:
What could be causing the Cannot POST /api/user/register error?
Are there any specific configurations I might be missing in my setup?
Could the issue be related to the Windows firewall or network settings?
Any other suggestions to troubleshoot and resolve this issue?
Console Logs:
Server is running on http://localhost:4000
database connected
Received registration request { email: ‘[email protected]’, name: ‘Anirudh’, password: ‘password’ }
Any help or pointers would be greatly appreciated!
help me even tried thunder client
Aniruddh tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.