rapidcat v1.0.3
Rapidcat
Blazingly Fast, Exceptionally Lightweight, and Highly Flexible Web Framework for Node.js.
Overview
Rapidcat is a high-performance and lightweight web framework designed for Node.js applications. With its robust routing system and middleware support, Rapidcat enables developers to build scalable and maintainable web applications efficiently.
Features
- 🚀 High Performance: Optimized for speed and low latency.
- 🎯 Lightweight: Minimalistic core with essential features.
- 🔧 Flexible Routing: Simple yet powerful routing capabilities.
- 🛠 Middleware Support: Seamlessly integrate middlewares for enhanced functionality.
- 📦 Easy to Use: Intuitive API with a developer-friendly experience.
Installation
Rapidcat is available as an npm package and requires Node.js 10 or later.
npm install rapidcat
For more details, check out the installation guide.
Quick Start
Create a simple web server using Rapidcat:
const rapidcat = require('rapidcat');
const app = rapidcat();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Routing Examples
Rapidcat provides an intuitive routing system:
app.get('/users', (req, res) => {
res.json({ users: [] });
});
app.post('/users', (req, res) => {
res.status(201).json({ message: 'User created' });
});
app.put('/users/:id', (req, res) => {
res.json({ message: `User ${req.params.id} updated` });
});
app.delete('/users/:id', (req, res) => {
res.json({ message: `User ${req.params.id} deleted` });
});
Advanced Routing Example
const rapidcat = require('rapidcat');
const { prevMiddleware, nextMiddleware } = require('./middlewares');
const app = rapidcat();
const products = [
{ id: 1, name: "product-1" },
{ id: 2, name: "product-2" }
];
app.get('/products/:id', (req, res) => {
const productId = parseInt(req.params.id);
const product = products.find((p) => p.id === productId);
if (!product) return res.json({ error: 'Product not found' });
return res.json(product);
});
app.get('/products', prevMiddleware, (req, res, next) => {
return res.json(products);
}, nextMiddleware);
app.post('/products', (req, res) => {
const newProduct = req.body;
const maxId = Math.max(...products.map((p) => p.id));
newProduct.id = maxId + 1;
products.push(newProduct);
return res.json(newProduct);
});
app.put('/products/:id', (req, res) => {
const productId = parseInt(req.params.id);
const updatedProduct = req.body;
const index = products.findIndex((p) => p.id === productId);
if (index === -1) return res.status(404).json({ error: 'Product not found' });
products[index] = { ...products[index], ...updatedProduct };
return res.json(products[index]);
});
app.delete('/products/:id', (req, res) => {
const productId = parseInt(req.params.id);
const index = products.findIndex((p) => p.id === productId);
if (index === -1) return res.status(404).json({ error: 'Product not found' });
products.splice(index, 1);
return res.json({ message: 'Product deleted' });
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Middleware Examples
Easily integrate middlewares to handle request processing:
const logger = (req, res, next) => {
console.log(`Request received: ${req.method} ${req.url}`);
next();
};
app.use(logger);
Adding authentication middleware:
const authMiddleware = (req, res, next) => {
if (!req.headers.authorization) {
return res.status(401).send('Unauthorized');
}
next();
};
app.get('/protected', authMiddleware, (req, res) => {
res.send('This is a protected route');
});
API Reference
Routing Methods
app.get(path, handler)
Handles HTTP GET requests and returns a response.
app.post(path, handler)
Handles HTTP POST requests, typically for creating resources.
app.put(path, handler)
Handles HTTP PUT requests, used for updating existing resources.
app.delete(path, handler)
Handles HTTP DELETE requests to remove resources.
Middleware Handling
app.use(middleware)
Registers a global middleware function that is executed for every request.
Example:
app.use((req, res, next) => {
console.log('Middleware executed');
next();
});
Server Control
app.listen(port, callback)
Starts the HTTP server and listens for incoming requests on the specified port.
Example:
app.listen(4000, () => {
console.log('Server running on port 4000');
});
Contributing
We welcome contributions of all kinds! To contribute, please check out the Contributing Guide.
Security Issues
If you find a security vulnerability, please refer to our Security Policies and Procedures.
Author
Rapidcat is developed and maintained by Ferhat Yalçın.