Creating API within Next.js
mongoose is used for creating database schema, we create a user model
1
2
3
4
5
6
7
8
9
10
11
12
|
import mongoose from "mongoose";
const UserSchema = mongoose.Schema(
{
name: { type: String, required: true, trim: true },
email: { type: String, required: true, unique: true, index: true },
password: { type: String, required: true, trim: true },
},
{ timestamps: true }
);
export default mongoose.model("User", UserSchema);
|
creating utility file and defining all the necessary methods to handle the api response is the clean code we should follow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
export const errorHandler = (data, res, code = 400) => {
res.status(code).json({
hasError: true,
errorMessage: data,
});
};
export const responseHandler = (data, res, code = 200) => {
res.status(code).json({ hasError: false, body: data });
};
export const validateAll = (fields) => {
for (let key in fields) {
if (fields[key].trim() === "") throw `${key} is required`;
}
};
|
and finally we can create our signup folder under pages/api in order to use Next.js indigenous routing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import { dbConnect } from "../../../lib/db-connect";
import User from "../../../src/models/user";
import {
errorHandler,
responseHandler,
validateAll,
} from "../../../src/utils/common";
export default async function handler(req, res) {
if (req.method !== "POST") errorHandler("Invalid request", res);
try {
const { name, email, password } = req.body;
validateAll({ name, email, password });
// create db connection
await dbConnect();
//create new User object and saved it to db
const user = new User({ name, email, password });
const savedUser = await user.save();
if (savedUser) responseHandler(savedUser, res, 201);
else errorHandler("something went wrong", res);
//catching error then finally closing connection
} catch (error) {
errorHandler(error, res);
} finally {
res.end();
}
}
|