lib-server
v1.2.1
Published
Uma biblioteca simples e leve para criar servidores HTTP em Node.js
Downloads
40
Readme
LibServer
LibServer is a lightweight HTTP server library for Node.js, designed as a simple alternative to Express.js. It has no external dependencies and provides features for routing, middleware management, and file uploads in a streamlined way.
Key Features
- Minimal HTTP Server: Built on Node.js's native
http
module. - Middleware Support: Easily apply global or route-specific middleware.
- Routing: Support for
GET
,POST
,PUT
,PATCH
, andDELETE
methods. - Query Parameters: Automatically parses query strings.
- File Uploads: Simple file upload handling with size and format restrictions.
- JSON Parsing: Native JSON request and response handling.
Installation
Install via npm:
npm install lib-server
Quick Start
Basic Example
Here’s how to create a simple server using LibServer:
import path from 'path';
import { Server } from 'lib-server';
import router from './routers.js';
const app = new Server();
// Middleware to handle JSON requests
app.use(app.json());
// Global middleware for logging requests
app.use((req, res, next) => {
console.log(`Method: ${req.method}, URL: ${req.url}`);
next();
});
// Define a GET route
app.get('/hello', (req, res) => {
res.end('Hello, World!');
});
// Configure file upload
const uploadSettings = {
format: '.png',
path: path.resolve('storage'),
maxFileSize: 1024 * 50 // 50 KB limit
};
// Define a POST route for file uploads
app.post('/upload', app.upload(uploadSettings), (req, res) => {
res.end('Upload successful!');
});
// Use an external router for the /run route
app.root('/run', router);
// Start the server on port 3000
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
export { app };
Using Routers
import { RootRouter } from 'lib-server';
const router = new RootRouter();
// GET route with query parameter
router.get('/test1', (req, res) => {
const { queryParam } = req.query;
res.send(`Test 1, queryParam: ${queryParam}`);
});
// GET route with dynamic parameter
router.get('/test2/:name', (req, res) => {
const { name } = req.params;
res.send(`Test 2, name: ${name}`);
});
// GET route with both query and dynamic parameters
router.get('/test3/:id', (req, res) => {
const { id } = req.params;
const { page } = req.query;
res.send(`Test 3, id: ${id}, page: ${page}`);
});
export default router;
API Documentation
use(middleware)
Adds a global middleware function. Example:
app.use((req, res, next) => {
console.log('Middleware executed');
next();
});
json()
Middleware for automatically parsing JSON payloads.
app.use(app.json());
upload(options)
Middleware for file uploads. Options include:
- format: File format to allow.
- path: Destination folder.
- maxFileSize: Maximum file size (in bytes).
app.post('/upload', app.upload({ format: '.jpg', path: 'uploads/', maxFileSize: 1024 * 100 }), handler);
get(path, ...middlewares)
Defines a GET
route. Example:
app.get('/hello', (req, res) => {
res.end('Hello, World!');
});
post(path, ...middlewares)
Defines a POST
route.
app.post('/data', (req, res) => {
res.json({ message: 'Data received' });
});
put(path, ...middlewares)
Defines a PUT
route for updating resources.
patch(path, ...middlewares)
Defines a PATCH
route for partial updates.
delete(path, ...middlewares)
Defines a DELETE
route for removing resources.
listen(port, callback)
Starts the server on a specific port and runs the callback function.
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Contributing
Contributions are welcome! Here's how you can contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and write tests (if possible).
- Submit a pull request explaining your changes.
Find the project repository here: GitHub - MendoncaGabriel/lib-server
License
This project is licensed under the MIT License. See the LICENSE file for more details.