Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firsttimeez/simple-api-router
A lightweight, flexible API routing library for JavaScript that provides a simple and extensible way to define and manage API Endpoints.
https://github.com/firsttimeez/simple-api-router
api backend dynamic endpoint execution extensible flexible framework handler lightweight router routing server simple
Last synced: 3 days ago
JSON representation
A lightweight, flexible API routing library for JavaScript that provides a simple and extensible way to define and manage API Endpoints.
- Host: GitHub
- URL: https://github.com/firsttimeez/simple-api-router
- Owner: FirstTimeEZ
- License: apache-2.0
- Created: 2024-12-09T03:17:09.000Z (17 days ago)
- Default Branch: main
- Last Pushed: 2024-12-09T03:42:04.000Z (17 days ago)
- Last Synced: 2024-12-09T04:22:23.725Z (17 days ago)
- Topics: api, backend, dynamic, endpoint, execution, extensible, flexible, framework, handler, lightweight, router, routing, server, simple
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/simple-api-router
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple API Router
A lightweight, flexible `API` routing library for `JavaScript` that provides a simple and extensible way to define and manage `API Endpoints`.
## Features
- Simple `API` and `Endpoint` class structure
- Easy route and method matching
- Flexible endpoint handler execution
- Supports dynamic API routing## Usage Example
This example is taken from a [`Full Working Example`](https://github.com/FirstTimeEZ/server-ssl)
Defining an `API`
```javascript
import { Api, Endpoint } from 'simple-api-router';const API = new Api("/api/");
API.addEndpoint(new Endpoint("time", "GET", (req, res) => {
return STATE.respondWithContent(res, Date.now().toString(), STATE.TEXT_HTML);
}));const HTTPS_SERVER = createServerHTTPS(STATE.loadDefaultSecureContext(), (req, res) => {
if (API.checkFindExecute(req, res)) {
return;
}
...
})...
```Calling an `API`
```javascript
fetch("/api/time").then((response) => response.json().then((time) => console.log(time)));
```## API Classes
### `Api` Class
#### Constructor
- `new Api(apiRoute)`: Create a new API with a base route#### Methods
- `addEndpoint(endpoint)`: Add an endpoint to the API
- `checkRoute(url)`: Check if a URL matches the API's base route
- `findEndpoint(endpointPath)`: Find an endpoint matching a given path
- `findEndpointCheckMethod(req)`: Find an endpoint and verify its HTTP method
- `checkFindExecute(req, res)`: Attempt to execute the handler for a matching endpoint### `Endpoint` Class
#### Constructor
- `new Endpoint(path, method, handler)`: Create a new endpoint#### Methods
- `checkMethod(method)`: Verify the HTTP method
- `executeHandler(req, res)`: Execute the endpoint's handler
- `executeHandlerArgs(req, res, ...args)`: Execute the handler with additional arguments## Key Concepts
- The `Api` class manages a collection of endpoints for a specific route prefix
- `Endpoint` instances define specific path, HTTP method, and handler combinations
- `checkFindExecute` method provides a convenient way to route and handle requests