Logo

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
Categories
3 categories in total
middleware
open
node
open
express
open
Author
25 person written this
steve_adolf_c8df300913217
open
I Really like Middleware in NodeJs/Express.

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" });
  }
};

Enter fullscreen mode Exit fullscreen mode
middleware Article's
30 articles in total
Favicon
I Really like Middleware in NodeJs/Express.
Favicon
CSRF tokens in Nextjs
Favicon
Create Your Own Middleware Application Using Chain of Responsibility
Favicon
Rust: Demystifying Middleware in Actix Web
Favicon
Apache Camel - The Integration Framework for Modern Applications
Favicon
Express.js Applications with Middleware
Favicon
Nostr: a Better Future for Authentication
Favicon
Building Secure, Scalable, and Low-Latency IoT Middleware
Favicon
How to Build Scalable Middleware with Express.js
Favicon
How to quickly add API Key validation to a Node Express API
Favicon
Middleware in Lithe: How It Works and How to Create Your Own
Favicon
Middleware no Lithe: Como Funciona e Como Criar o Seu PrΓ³prio
Favicon
πŸ—ΊοΈ Peta Jalan Laravel: Menjelajah Routing, Middleware, dan Controller (Indonesian Version)
Favicon
Centralized Exception Handling in ASP.NET Core - Custom Middleware
Favicon
Adding Logging and Error Handling Middleware to Your Go API
Favicon
A Simple Way to Handle Locale-Specific URLs in Express
Favicon
Entendendo e Implementando Middlewares no Express.js
Favicon
Deep Dive ASP.NET Core Middleware : Part 1
Favicon
Next.js Middleware: Simple Guide to Control Requests
Favicon
Building a middleware with Nextjs
Favicon
Extending Prisma ORM with Custom Middleware: Logging, Validation, and Security
Favicon
Understanding NestJS Middleware
Favicon
Implementing Secure Authentication in Next.js with JWT and MongoDB. Protect Routes using middleware
Favicon
Middleware function Execution Problem and Solution
Favicon
Securing Next.js APIs with Middleware Using Environment Variables
Favicon
Middleware and Interceptors in NestJS: Best Practices
Favicon
NodeJS Security Middlewares
Favicon
Middleware to Asp.net Core MVC Application + Custom Middleware
Favicon
Implementing CORS in a Custom Next.js Server
Favicon
How to Set Up Signup, Login, and Logout using Django's Middleware

Featured ones: