This is a template for backend projects using expresss
and mongoose
Rolled out with ❤️ by Kavii Suri - Black Marbles Tech
- Use the
asyncHandler
function from theexpress-async-handler
to wrap async express routes (this is done due to error handling purposes)- example
// async route router.get('/async-route', asyncHandler(async (req, res) => { // Normal Express Route Code }))
- example
- Routes are supposed to be made using the express routers each of them being a seperate file in the routes folder.
- All routers must be used by the index router
- Errors are cought automatically when you throw them using Node JS Error
- To define statusCode of an error, add a property called
statusCode
- example
// sync error router.get('/sync-error', (req, res) => { const error = new Error("Sync Error Handling working") error.statusCode = 500 throw error })
- example
- For Async Errors, the procedure is the same (Just Use the
asyncHandler
as told above)- example
// async error router.get('/async-error', asyncHandler(async (req, res) => { const error = new Error("Async Error Handling working") error.statusCode = 500 throw error }))
- example