node-router-js
v1.0.0
Published
Simple Router is a minimalistic, lightweight routing library for Node.js. It allows you to easily define routes for HTTP methods and supports dynamic route parameters. Ideal for small projects, learning purposes, or microservices.
Readme
Simple Router
Simple Router is a minimalistic, lightweight routing library for Node.js. It allows you to easily define routes for HTTP methods and supports dynamic route parameters. Ideal for small projects, learning purposes, or microservices.
Installation
npm install simple-router or, if you want to use it locally: npm install /path/to/simple-router
npm install simple-routeror, if you want to use it locally:
npm install /path/to/simple-routerUsage
import http from 'http';
import Router from 'simple-router';
const app = new Router();
// Simple route
app.get('/', (req, res) => {
res.end('Hello, world!');
});
// Dynamic route with parameters
app.get('/users/:id', (req, res, params) => {
res.end(`User ID: ${params.id}`);
});
// Handle requests
const server = http.createServer((req, res) => app.handle(req, res));
server.listen(8081, () => {
console.log('Server running at http://127.0.0.1:8081/');
});
API
Router Creates a new Router instance.
const router = new Router();add(method, path, handler)
Adds a new route with the specified HTTP method.
method — HTTP method (GET, POST, PUT, DELETE)
path — route path (supports dynamic parameters, e.g., /users/:id)
handler — function (req, res, params) => {}