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.
Key Differences
Section titled “Key Differences”- Callback vs Return: Fastify uses
reply.send(). Shokupan uses direct returns andctx.send(),ctx.json(),ctx.text(), etc. - Context: Fastify splits
requestandreply. Shokupan combines them intoctx. - Schemas: Fastify uses JSON Schema by default. Shokupan is validator-agnostic but integrates well with Zod and other validators via the Validation plugin.
Basic Server
Section titled “Basic Server”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);Middleware / Hooks
Section titled “Middleware / Hooks”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"); } }});Validation
Section titled “Validation”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 };});Next Steps
Section titled “Next Steps”- Performance - Performance tips
- Plugins - Explore the ecosystem