mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(api): example middleware (#48434)
* feat(api): add middleware example * feat(api): add `@fastify/middie`, reorder for alphabetness * [skip-ci] [skip ci]
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
6d46f61fe9
commit
f7644bec68
+13
-3
@@ -1,8 +1,10 @@
|
||||
import { config } from 'dotenv';
|
||||
config({ path: '../.env' });
|
||||
import Fastify from 'fastify';
|
||||
import middie from '@fastify/middie';
|
||||
import { testRoutes } from './routes/test';
|
||||
import { dbConnector } from './db';
|
||||
import { testMiddleware } from './middleware';
|
||||
|
||||
const fastify = Fastify({
|
||||
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
|
||||
@@ -12,10 +14,18 @@ fastify.get('/', async (_request, _reply) => {
|
||||
return { hello: 'world' };
|
||||
});
|
||||
|
||||
void fastify.register(dbConnector);
|
||||
void fastify.register(testRoutes);
|
||||
|
||||
const start = async () => {
|
||||
// NOTE: Awaited to ensure `.use` is registered on `fastify`
|
||||
await fastify.register(middie);
|
||||
|
||||
// @ts-expect-error Types are not exported from Fastify,
|
||||
// and TypeScript is not smart enough to realise types
|
||||
// defined within this module have the same signature
|
||||
void fastify.use('/test', testMiddleware);
|
||||
|
||||
void fastify.register(dbConnector);
|
||||
void fastify.register(testRoutes);
|
||||
|
||||
try {
|
||||
const port = Number(process.env.PORT) || 3000;
|
||||
fastify.log.info(`Starting server on port ${port}`);
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
import { NextFunction } from '../utils';
|
||||
|
||||
export async function auth0Verify() {
|
||||
// Verify user authorization code with Auth0
|
||||
}
|
||||
|
||||
export function testMiddleware(
|
||||
req: Request,
|
||||
_res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
//
|
||||
console.log('Test middleware running');
|
||||
console.log(req.headers);
|
||||
next();
|
||||
}
|
||||
|
||||
+25
-16
@@ -1,31 +1,40 @@
|
||||
{
|
||||
"name": "@freecodecamp/api",
|
||||
"version": "0.0.1",
|
||||
"author": "freeCodeCamp <team@freecodecamp.org>",
|
||||
"bugs": {
|
||||
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/middie": "8.0.0",
|
||||
"@fastify/mongodb": "6.1.0",
|
||||
"fastify": "4.9.2",
|
||||
"fastify-plugin": "4.3.0"
|
||||
},
|
||||
"description": "The freeCodeCamp.org open-source codebase and curriculum",
|
||||
"license": "BSD-3-Clause",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"node": ">=18",
|
||||
"npm": ">=8"
|
||||
},
|
||||
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
|
||||
"license": "BSD-3-Clause",
|
||||
"main": "none",
|
||||
"name": "@freecodecamp/api",
|
||||
"nodemonConfig": {
|
||||
"env": {
|
||||
"NODE_ENV": "development"
|
||||
},
|
||||
"ignore": [
|
||||
"**/*.js"
|
||||
]
|
||||
},
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/freeCodeCamp/freeCodeCamp.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
|
||||
},
|
||||
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
|
||||
"author": "freeCodeCamp <team@freecodecamp.org>",
|
||||
"main": "none",
|
||||
"scripts": {
|
||||
"build": "tsc index.ts",
|
||||
"start": "NODE_ENV=production node index.js",
|
||||
"develop": "NODE_ENV=development nodemon index.ts"
|
||||
"develop": "nodemon index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/mongodb": "6.1.0",
|
||||
"fastify": "4.9.2",
|
||||
"fastify-plugin": "4.3.0"
|
||||
}
|
||||
"version": "0.0.1"
|
||||
}
|
||||
|
||||
@@ -13,3 +13,7 @@ function sha256(buf: Buffer) {
|
||||
return createHash('sha256').update(buf).digest();
|
||||
}
|
||||
export const challenge = base64URLEncode(sha256(Buffer.from(verifier)));
|
||||
|
||||
// This is used for Fastify middleware, but is not exported from Fastify itself.
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type NextFunction = (err?: any) => void;
|
||||
|
||||
Reference in New Issue
Block a user