This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Approach 1: Express middleware to add headers to all responses | |
export const addHeaders = (headers = {}) => { | |
return (req, res, next) => { | |
// Store original res.json and res.send methods | |
const originalJson = res.json.bind(res); | |
const originalSend = res.send.bind(res); | |
// Override res.json to add headers | |
res.json = function(data) { | |
// Add custom headers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server.ts | |
import express, { Request, Response, NextFunction } from "express"; | |
import type { RequestHandler } from "express"; | |
// ---- Types ---- | |
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; | |
export interface HandlerDef { | |
method: HttpMethod; | |
handler: RequestHandler; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Router, Request, Response } from 'express'; | |
const router = Router(); | |
// Handler for GET /v1/credit | |
const getCredit = (req: Request, res: Response) => { | |
res.json({ message: 'Retrieved credit information' }); | |
}; | |
// Handler for POST /v1/credit |