A robust, TypeScript-first HTTP router for Node.js with comprehensive middleware support. Routify is built for developers who need a lightweight yet formidable routing solution with strong type safety.
At the moment, Routify has support for these:
- π― Full TypeScript support
- π Global and route-specific middleware
- β‘οΈ Async/await support
- π¨ Clean, chainable API
- π URL parameter parsing
- π Automatic body parsing for
POST/PUT/PATCH - π¦ Zero dependencies
- π‘οΈ Built-in error handling
npm installation, at the moment.
npm install routifyimport { Routify } from 'routify';
const app = new Routify(3000); // Port optional, defaults at 3000
// Global middleware example
app.use(async (req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.pathname}`);
await next();
});
// Routes with middleware
app.get('/api/testingroutify', async (req, res) => {
res.json({ message: 'Testing Routify!' });
});
app.get('/api/users/:id',
// Route-specific middleware
async (req, res, next) => {
const token = req.getHeader('Authorization');
if (!token) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
await next();
},
async (req, res) => {
const { id } = req.params;
res.json({ userId: id });
}
);
app.start(() => {
console.log('π Routify is running on port 3000');
});import { Routify } from 'routify';
const app = new Routify();// Global middleware
app.use(async (req, res, next) => {
// Middleware logic
await next();
});
// Route-specific middleware
app.get('/path',
middleware1,
middleware2,
handler
);app.get(path: string, ...handlers: (RouteHandler | Middleware)[]);
app.post(path: string, ...handlers: (RouteHandler | Middleware)[]);
app.put(path: string, ...handlers: (RouteHandler | Middleware)[]);
app.delete(path: string, ...handlers: (RouteHandler | Middleware)[]);interface RoutifyRequest {
params: Record<string, string>; // URL parameters
query: Record<string, string>; // Query string parameters
body: any; // Parsed request body
parseBody(): Promise<void>; // Parse request body
getHeader(name: string): string | undefined;
method: string;
pathname: string;
}interface RoutifyResponse {
status(code: number): RoutifyResponse;
json(data: any): void;
send(data: string): void;
setHeader(name: string, value: string): RoutifyResponse;
}Support for URL parameters using :param syntax:
app.get('/api/users/:id', async (req, res) => {
const { id } = req.params;
res.json({ userId: id });
});Query parameters are automatically parsed and accessible via req.query:
// GET /api/search?q=test&sort=desc
app.get('/api/search', async (req, res) => {
const { q, sort } = req.query;
res.json({ searchTerm: q, sortOrder: sort });
});Automatic body parsing for POST/PUT/PATCH requests:
app.post('/api/users', async (req, res) => {
await req.parseBody();
const userData = req.body;
res.status(201).json({
message: 'User created',
user: userData
});
});Comprehensive error handling for both middlewares and routes:
// Global error handling middleware
app.use(async (req, res, next) => {
try {
await next();
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});Basic REST API With Auth:
const authMiddleware = async (req, res, next) => {
const token = req.getHeader('Authorization');
if (!token) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
await next();
};
app
.get('/api/testingroutify', async (req, res) => {
res.json({ message: 'Testing Routify!' });
})
.get('/api/users/:id',
authMiddleware,
async (req, res) => {
const { id } = req.params;
res.json({ userId: id, data: 'User data here' });
}
)
.post('/api/users', async (req, res) => {
await req.parseBody();
res.status(201).json({
message: 'User created',
user: req.body
});
});Contributions? Please feel free to submit a Pull Request. Check out our contributing guidelines for more information.
Happy Routing With Routify! π