morpress.js
Version:
A simple API framework without dependencies
138 lines (101 loc) • 3.75 kB
Markdown
---
# Morpress.js
`Morpress.js` is a lightweight and straightforward API framework for Node.js. It provides core functionalities for creating HTTP servers with custom routing, static file serving, rate limiting, and basic security measures, all without relying on Express.
## Features
- **Routing:** Define routes for HTTP methods (`GET`, `POST`, `PUT`, `DELETE`, `PATCH`)
- **Static File Serving:** Serve static files from specified directories
- **Rate Limiting:** Prevent abuse by limiting requests from a single IP
- **Security:** Basic security enhancements with Helmet
- **Middleware Support:** Use global middleware for request processing
- **Error Handling:** Custom error handling
## Installation
Install the necessary dependencies using npm:
```bash
npm install morpress.js
```
## Usage
### Basic Setup
Here’s a basic example to get started:
```javascript
const { Morpress, Router } = require('morpress.js');
// Create an instance of Morpress.js
const app = new Morpress();
// Configure rate limiting
app.rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // Maximum 100 requests
});
// Add middleware
app.use((req, res, next) => {
console.log(`Received request: ${req.method} ${req.url}`);
next();
});
// Define routes
app.get('/hello', (req, res) => {
res.json({ message: 'Hello, world!' });
});
app.post('/data', (req, res) => {
res.json({ received: req.body });
});
// Serve static files
app.static('./public');
// Use a Router
const router = new Router();
router.get('/nested', (req, res) => {
res.json({ message: 'Nested route' });
});
app.use('/api', router);
// Set error handler
app.setErrorHandler((err, req, res) => {
res.status(500).json({ error: 'Something went wrong!' });
});
// Start the server
app.listen(3000, () => {
console.log('Server running on port 3000');
});
```
### Rate Limiting
Rate limiting helps control the number of requests an IP address can make in a given timeframe. Configure it with:
```javascript
app.rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // Maximum 100 requests per window
});
```
### Static File Serving
To serve static files, specify directories using the `static` method:
```javascript
app.static('./public');
```
### Error Handling
Define a custom error handler to manage errors:
```javascript
app.setErrorHandler((err, req, res) => {
res.status(500).json({ error: 'Something went wrong!' });
});
```
## API
### `Morpress` Class
- `use(path, middlewareOrRouter)`: Add global middleware or Router.
- `get(path, handler)`: Define a route for GET requests.
- `post(path, handler)`: Define a route for POST requests.
- `put(path, handler)`: Define a route for PUT requests.
- `delete(path, handler)`: Define a route for DELETE requests.
- `patch(path, handler)`: Define a route for PATCH requests.
- `static(path)`: Serve static files from the specified directories.
- `trustProxy(value)`: Configure proxy settings.
- `sessionOptions(options)`: Configure session settings.
- `rateLimit(options)`: Set rate limiting options.
- `setErrorHandler(handler)`: Define a custom error handler.
- `listen(port, callback)`: Start the server on the specified port.
### `Router` Class
- `use(path, middleware)`: Add middleware to the Router.
- `get(path, handler)`: Define a route for GET requests.
- `post(path, handler)`: Define a route for POST requests.
- `put(path, handler)`: Define a route for PUT requests.
- `delete(path, handler)`: Define a route for DELETE requests.
- `patch(path, handler)`: Define a route for PATCH requests.
- `handle(req, res)`: Handle incoming requests.
## License
This project is licensed under the MIT License. See the `LICENSE` file for more details.
---