Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jacovinus/pepper-router
Pure node and typescript
https://github.com/jacovinus/pepper-router
api context nodejs router typescript
Last synced: 8 days ago
JSON representation
Pure node and typescript
- Host: GitHub
- URL: https://github.com/jacovinus/pepper-router
- Owner: jacovinus
- License: gpl-3.0
- Created: 2024-02-03T16:32:52.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-07-10T15:25:33.000Z (4 months ago)
- Last Synced: 2024-07-10T18:42:54.961Z (4 months ago)
- Topics: api, context, nodejs, router, typescript
- Language: TypeScript
- Homepage:
- Size: 364 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Pepper Router
[![CodeQL](https://github.com/jacovinus/pepper-router/actions/workflows/CodeQL.yml/badge.svg?branch=main)](https://github.com/jacovinus/pepper-router/actions/workflows/CodeQL.yml)
🚧 Warning: this is just for fun, not to take it seriously at all.
Just some playing with pure node and typescript (avoiding frameworks)
## Current features:
### Context
CRUD data into memory---
### Router
- By default it writes data into memory through context
- It could write the context entries into file
- It could open the written file and return it's content
- Routes are listed at welcome screen#### Usage:
Writing a new Route:
At `/router/routes.ts`:
- add a new route following this example:
```js
addRouteToContext("GET", READ_LOGS_ROUTE, "read logs from file");
routes.get(READ_LOGS_ROUTE, (req, res) => readLogsFromFileHandler(req, res));
```
- add the `addRouteToContext` helper if you want to list it at welcome screen
- register the route with its method, path and handlerAt `/handler/index.ts`:
- add a new handler following this examples:
```js
// [GET] (no params)
const listRoutesHandler: Handler = (req, res) => {
const routes = listRoutesFromContext();
res?.writeHead(200, { "Content-Type": "application/json" });
res?.write(JSON.stringify(routes));
res?.end();
};// [GET] with params
const searchItemHandler: Handler = (req, res) => {
const url = new URL(req?.url || "/", "http://localhost");
const searchParams = new URLSearchParams(url?.searchParams);
const id = searchParams.get("id") as string;
const item = context.get(id);
res?.writeHead(200, { "Content-Type": "application/json" });
res?.write(JSON.stringify(item));
res?.end();
};// [POST]
const addItemHandler: Handler = (req, res) => {
const body = [] as Buffer[];
req.on("data", (chunk: any) => body.push(chunk));
req.on("end", () => {
const item = JSON.parse(Buffer.concat(body).toString());
context.add(item);
res?.writeHead(200, { "Content-Type": "application/json" });
res?.write(JSON.stringify(item));
res?.end();
});
};```
You should use the native request and response functionality from the `node:http` package.
For handling the data you could make use of the [context tools](./context/ctx.ts)
---
### Logs
- Writes logs at context at every request
- Could write logs into a file