Skip to content

Commit

Permalink
utils code discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Nov 6, 2023
1 parent 110e59c commit fe28708
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PORT=8000
MONGODB_URI=mongodb+srv://hitesh:[email protected]
MONGODB_URI=mongodb+srv://hitesh:[email protected]
CORS_ORIGIN=*
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"prettier": "^3.0.3"
},
"dependencies": {
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongoose": "^8.0.0"
Expand Down
18 changes: 18 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express from "express"
import cors from "cors"
import cookieParser from "cookie-parser"

const app = express()

app.use(cors({
origin: process.env.CORS_ORIGIN,
credentials: true
}))

app.use(express.json({limit: "16kb"}))
app.use(express.urlencoded({extended: true, limit: "16kb"}))
app.use(express.static("public"))
app.use(cookieParser())


export { app }
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ dotenv.config({


connectDB()
.then(() => {
app.listen(process.env.PORT || 8000, () => {
console.log(`⚙️ Server is running at port : ${process.env.PORT}`);
})
})
.catch((err) => {
console.log("MONGO db connection failed !!! ", err);
})



Expand Down
24 changes: 24 additions & 0 deletions src/utils/ApiError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class ApiError extends Error {
constructor(
statusCode,
message= "Something went wrong",
errors = [],
statck = ""
){
super(message)
this.statusCode = statusCode
this.data = null
this.message = message
this.success = false;
this.errors = errors

if (statck) {
this.stack = statck
} else{
Error.captureStackTrace(this, this.constructor)
}

}
}

export {ApiError}
8 changes: 8 additions & 0 deletions src/utils/ApiResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ApiResponse {
constructor(statusCode, data, message = "Success"){
this.statusCode = statusCode
this.data = data
this,message = message
this.success = statusCode < 400
}
}
27 changes: 27 additions & 0 deletions src/utils/asyncHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const asyncHandler = (requestHandler) => {
(req, res, next) => {
Promise.resolve(requestHandler(req, res, next)).catch((err) => next(err))
}
}


export {asyncHandler}




// const asyncHandler = () => {}
// const asyncHandler = (func) => () => {}
// const asyncHandler = (func) => async () => {}


// const asyncHandler = (fn) => async (req, res, next) => {
// try {
// await fn(req, res, next)
// } catch (error) {
// res.status(err.code || 500).json({
// success: false,
// message: err.message
// })
// }
// }

0 comments on commit fe28708

Please sign in to comment.