dev-resources.site
for different kinds of informations.
I Really like Middleware in NodeJs/Express.
Published at
1/11/2025
Categories
middleware
node
express
Author
steve_adolf_c8df300913217
Author
25 person written this
steve_adolf_c8df300913217
open
Middleware to attach a user to the request object because nodejs/express does not do that for you out of the box.
Say i have a controller that need to check whether a user is of certain role in order to let them access a certain resource. Then this middleware will be helpful as it makes a user available to this function. Then i can do something like user.role === <some_role> ? do_something: do_this
Here is how i might implement it.
/**
* @param {Request} req
* @param {Response} res
*/
const userIsAuthenticatedMiddleware = async (req, res, next) => {
const token = req.headers["authorization"]?.split(" ")[1];
if (!token) return res.status(401).json({ message: "Access denied" });
try {
jwt.verify(token, process.env.JWT_SECRET, (error, user) => {
if (error) return res.status(401).json({ message: "Wrong token" });
req.user = user;
next();
});
} catch (error) {
return res.status(500).json({ message: "Internal Server Error" });
}
};
node Article's
30 articles in total
Breaking the Scale Barrier: 1 Million Messages with NodeJS and Kafka
read article
assert in Nodejs and its usage in Grida source code
read article
Understanding Node.js Cluster: The Core Concepts
read article
🌟 A New Adventure Begins! 🛵🍕
read article
How “Silicon Valley” Inspired Me to Create a Photo Compressor CLI for Web Developers
read article
How Does This Jewelry Customization Site Work? Need Insights!
read article
Building a Secure Authentication API with TypeScript, Node.js, and MongoDB
read article
Understanding OAuth 1.0a Signature Generation: Postman vs. Node.js Library and Custom Implementation
read article
How to Fix the “Record to Delete Does Not Exist” Error in Prisma
read article
[Boost]
read article
Run NextJS App in shared-hosting cPanel domain!
read article
Construindo uma API segura e eficiente com @fastify/jwt e @fastify/mongodb
read article
New ways to engage with the stdlib community!
read article
Sore Throat: Causes, Symptoms, and Treatment
read article
Back to MonDEV 2025
read article
🌟 How to Fix Node.js Path Issues in VS Code (Step-by-Step Guide)
read article
How to write unit tests and E2E tests for NestJS applications
read article
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
read article
Exploring TypeScript Support in Node.js v23.6.0
read article
Mastering Backend Node.js Folder Structure, A Beginner’s Guide
read article
how to setup express api from scratch
read article
Load Balancing Node.js Applications with Nginx Upstream Configuration
read article
Using LRU Cache in Node.js and TypeScript
read article
Welcome to Siitecch! Your Go-To Platform for Beginner-Friendly Full-Stack JavaScript Learning.
read article
I Really like Middleware in NodeJs/Express.
currently reading
Your own Telegram bot on NodeJS with TypeScript, Telegraf and Fastify (Part 3)
read article
Understanding Node.js Cluster: The Core Concepts
read article
JWT Authentication With NodeJS
read article
Stream de Arquivo PDF ou Imagem S3 - AWS
read article
Understanding Node.js Alpine Versions: A Lightweight Choice for Your Projects
read article
Featured ones: