Create a route file “auth.route.js” in “routes” folder.
auth.route.js: –
import express from ‘express’
import { signup } from ‘../controllers/auth.controller.js’;
const router=express.Router();
router.post(‘/signup’, signup);
export default router;
——-
Create a file “auth.controller.js” in “controllers” folder.
auth.controller.js:
import User from ‘../models/user.model.js’
export const signup=async(req, res)=>{
const {username, email, password}=req.body;
if(!username || !email || !password || username===” || email===” || password===”){
return res.status(400).json({message:”All Fields are Required!”})
}
const newUser=new User({
username,
email,
password
})
await newUser.save();
res.json(“Signup Successfull”);
}
Import auth.route.js in index.js –
app.use(express.json());
app.use(‘/api/auth’, authRoute)
Test API with Postman
Video Tutorial: