try_catch_req

A module to make clear and succinct a redundant async/await try-catchs in using promises in node routes.

Usage no npm install needed!

<script type="module">
  import tryCatchReq from 'https://cdn.skypack.dev/try_catch_req';
</script>

README

try-catch

handling callbacks with async/await on node js server req,res pipeline without managing

try{ 
// the progrm that will cause exception 
}catch(){
// handling when the exception has happened
}

Just wrape your req,res pipeline with a tryCatch method by importing from this library, then it handles the rest.

Install the dependency

npm install try_catch_req

Example

const express=require('express');
const tryCatch=require('./index')
const app=express();

app.use(express.json())
app.post('/test',tryCatch(async(req,res,next)=>{
    const {name,job}=req.body;
    // you can call any async functions with await keyword,
    // Incase if exception throws that function the library handles automatically.
    res.status(201).json({message :`Well come ${name}, Are you really a ${job}?`})}));
module.exports=app;