I’m new to MERN stack. while I was trying to do a simple register endpoint, I’m getting the below error in the command prompt, can someone try to help me out of this please, Thanks in advance
Screenshot of my terminal
index.js
const express = require("express");
const bodyParser = require("body-parser");
const crypto = require("crypto");
const nodemailer = require("nodemailer");
const app = express();
const port = 8000;
const cors = require("cors");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const jwt = require("jsonwebtoken");
const mongoose = require("mongoose");
mongoose
.connect("mongodb://192.168.29.166/ecommerce-project", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("connected to mongodb");
})
.catch((err) => {
console.log("error connecting to mongodb", err);
});
app.listen(port, "192.168.29.166", () => {
console.log("server is running on port 8000");
});
const User = require("./models/user");
const Order = require("./models/order");
//function to send verification email to the user
const sendVerificationEmail = async (email, verificationToken) => {
// create a nodemailer trasnsport
const trasnsporter = nodemailer.createTransport({
//configure the email service
service: "gmail",
auth: {
user: "test@gmaill.com",
pass: "ktyhihvqrwmqcpex",
},
});
//compose the email message
const mailOptions = {
from: "amazon.com",
to: email,
subject: "Email Verification",
text: `Please click the following link to verify your email : http://localhost:8000/verify/${verificationToken}`,
};
// send the email
try {
await trasnsporter.sendMail(mailOptions);
} catch (error) {
console.log("Error sending verification email", error);
}
};
//endpoint to register in the app
app.post("/register", async (req, res) => {
try {
const { name, email, password } = req.body;
//Chech if The email is already registered
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: "Email already Registered" });
}
//create a new User
const newUser = new User({ name, email, password });
//generate and store the verification token
newUser.verificationToken = crypto.randomBytes(20).toString("hex");
//save the user to the database
await newUser.save();
//send verification email to the user
sendVerificationEmail(newUser.email, newUser.verificationToken);
} catch (error) {
console.log("error registering user", error);
res.status(500).json({ message: "Registration failed" });
}
});
//endpoint tp verify the email
app.get("/verify/token", async (req, res) => {
try {
const token = req.params.token;
//FInd the user with the given verification token
const user = await User.findOne({ verificationToken: token });
if (!user) {
return res.status(404).json({ message: "Invalid verification token" });
}
//Mark the user as verified
user, (verified = true);
user.verificationToken = undefined;
await user.save();
res.status(200).json({ message: "Email verified" });
} catch (error) {
res.status(500).json({ message: "Email veridication failed" });
}
});
user.js
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
verified: {
type: Boolean,
default: false,
},
verificationToken: String,
addresses: [
{
name: String,
mobileNo: String,
houseNo: String,
street: String,
landmark: String,
city: String,
country: String,
postalCode: String,
},
],
orders: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Order",
},
],
createdAt: {
type: Date,
default: Date.now,
},
});
const User = mongoose.model("User", userSchema);
module.exports = User;
package.json
{
"name": "api",
"version": "1.0.0",
"description": "backend",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"mongoose": "^7.4.3",
"nodemailer": "^6.9.4",
"nodemon": "^3.0.1"
}
}