Skip to content

Files

Latest commit

 

History

History
80 lines (64 loc) · 2.32 KB

readme.md

File metadata and controls

80 lines (64 loc) · 2.32 KB

Complete guide for router and controller with debugging

Logic Building :- spilting a big problems into smaller problems and solving them one by one

  • some by working on real world projects [80%]
  • some by leetcode problems

Let's Start

  • Make a new file user.controller.js in the controlller folder of src folder
    • let's code
      • import required methods like asyncHandler
      • write the method
        • this method is doing nothing but sending a ok response with 200 status code
import { asyncHandler } from "../utils/asyncHandler.js";


const registerUser = asyncHandler( async (req , res)=>{

    res.status(200).json(
        {
            message:"ok"
        }
    )
})
export {registerUser}
  • Making routes
    • when will this method registerUser will be called , it will be called when a certain url is hit
    • that url route is defined here
    • Go to routes folder and make a file user.routes.js
      • just making a router object and exporting
import { Router } from "express"

const router  = Router()

export default router 
  • Go to app.js now and write
    • so import the route and
    • app.use("/api/v1//users" , userRouter) states that when url hit at http://localhost:8000/api/v1/users it will go to userRouter and see the routes in that
//routes import
import  userRouter from './routes/user.routes.js'


// routes declaration
app.use("/users" , userRouter)
export { app }
import { Router } from "express"
import { registerUser } from "../controllers/user.controller.js"

const router  = Router()

router.route("/register").post(registerUser)
router.route("/#").post(loginUser)
  • Tools to test API

    • thunder client : vscode extension
    • postman : standard api testing tool
  • Download Postman and do basic # setup