Skip to content

Instantly share code, notes, and snippets.

View thebigredgeek's full-sized avatar
💭
Considering new contract opportunities at this time

Andrew E. Rhyne thebigredgeek

💭
Considering new contract opportunities at this time
View GitHub Profile
@thebigredgeek
thebigredgeek / contextclass.ts
Last active September 9, 2020 18:33
Context Class for Requests
import { Request } from 'express';
export default class Context {
static _bindings = new WeakMap<Request, Context>();
public foo = 'bar';
constructor () {}
static bind (req: Request) : void {
@thebigredgeek
thebigredgeek / middleware.ts
Created September 9, 2020 18:35
Context Middleware for Express
import express, { Request } from 'express';
import Context from './context'
const app = express();
// Middleware
app.use((req: Request, res: any, next: any) => {
Context.bind(req);
next();
});
import express, { Request } from 'express';
import Context from './context'
const app = express();
// ...
app.get('/foo', (req: Request, res) => {
const ctx: Context = Context.get(req);