express

javascript
function createReadiness() {
  let ready = true;

  function markUnready() {
    ready = false;
  }

Graceful HTTP Server Shutdown on SIGTERM With In-Flight Request Draining in Node.js

nodejs http graceful-shutdown
by codesnips 3 tabs
typescript
export type ErrorCode =
  | 'VALIDATION'
  | 'UNAUTHENTICATED'
  | 'FORBIDDEN'
  | 'NOT_FOUND'
  | 'CONFLICT'

API error shape that frontend can rely on

typescript express react
by codesnips 4 tabs
javascript
const jwt = require('jsonwebtoken');

const SECRET = process.env.JWT_SECRET;
const ALGORITHM = 'HS256';
const ACCESS_TTL = '15m';

JWT Authentication Middleware in Express That Populates req.user

express jwt authentication
by codesnips 3 tabs
javascript
const jwt = require('jsonwebtoken');

function authenticate(req, res, next) {
  const header = req.headers.authorization || '';
  const [scheme, token] = header.split(' ');

Role-Based Access Control in Express with a requireRole Middleware Factory

express middleware rbac
by codesnips 3 tabs
javascript
function parseRange(header, size) {
  if (!header || !header.startsWith('bytes=')) return null;

  const [rawStart, rawEnd] = header.replace('bytes=', '').split('-');
  let start;
  let end;

HTTP Range Requests for Video Streaming in Node.js With fs.createReadStream

nodejs http streaming
by codesnips 3 tabs
javascript
const express = require('express');
const controller = require('../controllers/userController');

const router = express.Router();

router.use((req, res, next) => {

Versioning an Express API with Router-per-Version Mounts and a Shared Controller Module

express api-versioning rest
by codesnips 4 tabs
javascript
const { z } = require('zod');

const signupSchema = z
  .object({
    email: z
      .string()

Reusable Zod Schema Validation Middleware for Express Signup Routes

express zod validation
by codesnips 3 tabs
typescript
import { S3Client } from "@aws-sdk/client-s3";
import { createPresignedPost, PresignedPost } from "@aws-sdk/s3-presigned-post";
import { randomUUID } from "crypto";

const s3 = new S3Client({ region: process.env.AWS_REGION });
const BUCKET = process.env.UPLOAD_BUCKET!;

Pre-signed S3 upload from the browser

s3 security aws-sdk
by codesnips 3 tabs
typescript
import type { Redis } from "ioredis";

export interface StoredResponse {
  status: "pending" | "completed";
  fingerprint: string;
  httpStatus?: number;

Idempotent POST Requests in Express with a Redis-Backed Middleware

express redis idempotency
by codesnips 3 tabs
typescript
export interface Cursor {
  createdAt: string;
  id: string;
}

export function encodeCursor(c: Cursor): string {

Cursor Pagination for a REST List Endpoint with a Typed Fetch Client

typescript express rest
by codesnips 3 tabs
typescript
import { RequestHandler } from 'express';
import { ZodTypeAny, ZodError } from 'zod';

export class ValidationError extends Error {
  status = 422;
  constructor(public issues: unknown) {

Composable Express Request Validation with Zod Schema Middleware

express validation zod
by codesnips 3 tabs
typescript
import { createHash } from "crypto";
import { Request, Response } from "express";

export function computeEtag(body: string): string {
  const digest = createHash("sha1").update(body).digest("base64");
  return `"${digest}"`;

ETag + conditional GET for read-heavy endpoints

performance express http-caching
by codesnips 3 tabs