Project
Deployed In
The Front End and the Back End are deployed in the Vercel.
Contains:
This template uses Next.Js + Mongoose + TailwindCSS. The site is deployed in Vercel.
How to use:
- Clone this repository or use the following code
https://github.com/kishore007k/next-with-mongoose.git
. - This will clone the repository to your system.
- Add your MongoDB connection string to the
MONGODB_URL
inside the next.config.js in the root directory. - To connect to the database you can either create a Model and use the
connectDB()
function to connect to the MongoDB database.
Commands:
npm run dev
# (or)
yarn build
MongoDB Database Connection:
The following code will let the user to connect to the MongoDB database. This is located inside the util/connectDb.js
in the root directory.
import mongoose from "mongoose"
const connectDB = handler => async (req, res) => {
if (mongoose.connections[0].readyState) {
// Use current db connection
return handler(req, res)
}
// Use new db connection
await mongoose.connect(process.env.MONGODB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
return handler(req, res)
}
export default connectDB
How to Connect to Database:
To connect to the Database you can simply call the connectDB()
function.
import bcrypt from "bcryptjs"
import connectDB from "../../utils/connectDB"
import UserModel from "../../models/userModel"
const handler = async (req, res) => {
switch (req.method) {
case "GET":
try {
const users = await UserModel.find({})
res.status(200).send({ data: users })
} catch (error) {
res.status(400).send({ error: error })
}
break
case "POST":
try {
const { userName, email, password, cPassword } = req.body
if (password !== cPassword) {
return res.send({ message: "Passwords doesn't match" })
}
const hashed = bcrypt.hashSync(password, 10)
const newUser = new UserModel({
userName,
email,
password: hashed,
})
const saved = await newUser.save()
if (saved) return res.send({ data: saved })
} catch (error) {
res.send({ error: error })
}
break
default:
res.status(400).send({ error: "Server Error" })
break
}
}
export default connectDB(handler)
Models
All the models are created in the Models folder in the root directory.
Example:
// User Model
import mongoose from "mongoose"
const userSchema = new mongoose.Schema(
{
userName: {
type: String,
required: true,
maxlength: 32,
},
email: {
type: String,
required: true,
trim: true,
index: { unique: true },
match: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
},
password: {
type: String,
required: true,
},
cPassword: {
type: String,
},
},
{
timestamps: true,
}
)
mongoose.models = {}
const UserModel = mongoose.model("users", userSchema)
export default UserModel
Middleware
All the verifyUser
, isLoggedIn
& isAdmin
checks should be defined in the Middlewares folder and exported.