glutonium
glutonium
Explore posts from servers
KPCKevin Powell - Community
Created by Duckit69 on 4/14/2025 in #help
JWT TOKEN
u also have to pass cookie options where you'll set secure to true or something like that
9 replies
KPCKevin Powell - Community
Created by Duckit69 on 4/14/2025 in #help
JWT TOKEN
res.status(200)
.json({})
.cookie("refToken", refToken)
.cookie("accToken", accToken)
res.status(200)
.json({})
.cookie("refToken", refToken)
.cookie("accToken", accToken)
9 replies
KPCKevin Powell - Community
Created by Duckit69 on 4/14/2025 in #help
JWT TOKEN
cookie parser is for handling cookies u can use that to easily set ref token and acc token securely in the clients browser
9 replies
KPCKevin Powell - Community
Created by Duckit69 on 4/14/2025 in #help
JWT TOKEN
u can use cookie parser lib for that
9 replies
KPCKevin Powell - Community
Created by Ganesh on 4/8/2025 in #help
Error handling practices in ExpressJS
errorHandler middleware
import { NextFunction, Request, Response } from "express";
import { ZodError } from "zod";

export const errorHandler = (err: any, _: Request, res: Response, __: NextFunction) => {

console.error(err);
if (err instanceof ZodError) {
res.status(400).json({
success: false,
message: "Validation error",
errors: err.errors.map(error => ({
path: error.path.join("."),
message: error.message,
})),
});
return;
}

res.status(err.status || 500).json({
success: false,
message: err.message || 'Internal Server Error',
});
};
import { NextFunction, Request, Response } from "express";
import { ZodError } from "zod";

export const errorHandler = (err: any, _: Request, res: Response, __: NextFunction) => {

console.error(err);
if (err instanceof ZodError) {
res.status(400).json({
success: false,
message: "Validation error",
errors: err.errors.map(error => ({
path: error.path.join("."),
message: error.message,
})),
});
return;
}

res.status(err.status || 500).json({
success: false,
message: err.message || 'Internal Server Error',
});
};
asyncHandler
import { Request, Response, NextFunction } from "express";

// so this is an async handler function and here's what it does
// takes a async callback function
// wraps in a try catch block
// returns the new function where the call back is wrapped in a try catch
// doing so we dont need to wrap every async func in a try catcn and instead just pass into this function
//
// now this is going to be used for wrappign the controllers
// these controllers r sent to the router http methods (router.path().get() || router.path().post())
// the http verb methods (get, post....) pass the req, res and next arguments
// hence u see those as parameter here in the asyncHandler
//
// so basically this just takes an async func and returns it in a new func wrapped in try catch
export function asyncHandler(
cb: (req: Request, res: Response, next: NextFunction) => Promise<any>
) {
return async function(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
await cb(req, res, next);
} catch (error) {
next(error);
}
}
}
import { Request, Response, NextFunction } from "express";

// so this is an async handler function and here's what it does
// takes a async callback function
// wraps in a try catch block
// returns the new function where the call back is wrapped in a try catch
// doing so we dont need to wrap every async func in a try catcn and instead just pass into this function
//
// now this is going to be used for wrappign the controllers
// these controllers r sent to the router http methods (router.path().get() || router.path().post())
// the http verb methods (get, post....) pass the req, res and next arguments
// hence u see those as parameter here in the asyncHandler
//
// so basically this just takes an async func and returns it in a new func wrapped in try catch
export function asyncHandler(
cb: (req: Request, res: Response, next: NextFunction) => Promise<any>
) {
return async function(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
await cb(req, res, next);
} catch (error) {
next(error);
}
}
}
10 replies
KPCKevin Powell - Community
Created by Ganesh on 4/8/2025 in #help
Error handling practices in ExpressJS
now i also make a helper function to wrap controllers in try catch (asyncHandler) where in the catch block i am dong next(error)
10 replies
KPCKevin Powell - Community
Created by Ganesh on 4/8/2025 in #help
Error handling practices in ExpressJS
in my backend the way i have it, is when there is an error , say in user registration controller where username format is invalid, i would simply throw new Error() or extend the Error class to make a custom ApiError class if i want some extra props and throw that. i have an error handler middleware which basically when an error is thrown , catches it, and makes a new api response with 500 status code by default otherwise what is provided (this is where extending Error class comes in handy cause u can add an extra status pop) and json body with msg and the status code and success: false (just personal pref)
10 replies
KPCKevin Powell - Community
Created by Arctomachine on 4/1/2025 in #help
How is your day today?
i hate the days i hate 😔
13 replies
KPCKevin Powell - Community
Created by 13eck on 3/3/2025 in #resources
C Implementation of Flexbox
He gives me code aesthetics vibe
2 replies
KPCKevin Powell - Community
Created by ghoul on 3/1/2025 in #back-end
Problem understanding Callback
do you have hard time understanding callbacks in general or do you have hard time understanding how to use callbacks in terms of promises specifically
2 replies
KPCKevin Powell - Community
Created by i_lost_to_loba_kreygasm on 2/24/2025 in #back-end
How to delete objects from `notifications` array after certain amount of time in mongoDB?
what if you have notifications as it's own document additionally with an author id
5 replies
KPCKevin Powell - Community
Created by i_lost_to_loba_kreygasm on 2/24/2025 in #back-end
How to delete objects from `notifications` array after certain amount of time in mongoDB?
maybe look at "mongodb TTL"
5 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
that's the main error
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
i want to know what console.error() logs out
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
i know how try catch works
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
you're saying the catch() block never gets ran?
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
i want to know what this logs out
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
i am talking about this like right here console.error("Error in registration:", error);
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
no worries
38 replies
KPCKevin Powell - Community
Created by saad khan on 2/20/2025 in #back-end
sending first data in mongodb
not the postman one but show the server output (console.error)
38 replies