Skip to content

Migrating from Fastify

Fastify is known for speed and schemas. Shokupan offers comparable performance and strong typing but with a Web Standards-based API and support for Controllers and DI.

  1. Callback vs Return: Fastify uses reply.send(). Shokupan uses direct returns and ctx.send(), ctx.json(), ctx.text(), etc.
  2. Context: Fastify splits request and reply. Shokupan combines them into ctx.
  3. Schemas: Fastify uses JSON Schema by default. Shokupan is validator-agnostic but integrates well with Zod and other validators via the Validation plugin.

Fastify:

import Fastify from 'fastify';
const fastify = Fastify();
fastify.get('/', async (request, reply) => {
return { hello: 'world' };
});
fastify.listen({ port: 3000 });

Shokupan:

import { Shokupan } from 'shokupan';
const app = new Shokupan();
app.get('/', (ctx) => {
return { hello: 'world' };
});
app.listen(3000);

Fastify uses Hooks (onRequest, preHandler). Shokupan supports both middleware and hooks.

Fastify:

fastify.addHook('onRequest', async (request, reply) => {
// Audit log
});

Shokupan: Middleware:

app.use(async (ctx, next) => {
// Pre-processing / Audit log
await next();
});

OR

Hooks:

const app = new Shokupan({
hooks: {
onRequestStart: (ctx) => {
console.log("Request started");
}
}
});

Fastify (JSON Schema):

fastify.get('/', {
schema: {
querystring: {
type: 'object',
properties: {
name: { type: 'string' }
}
}
}
}, handler)

Shokupan (Zod):

import { Validation } from 'shokupan';
import { z } from 'zod';
app.get('/', Validation(z.object({
name: z.string()
})), (ctx) => {
// ctx.body / ctx.query is typed
return { name: ctx.body.name };
});