Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thecommieaxolotl/pyotr
A tiny HTTP framework with inbuilt routing and middleware support.
https://github.com/thecommieaxolotl/pyotr
express http https javascript middleware nodejs router typescript
Last synced: about 3 hours ago
JSON representation
A tiny HTTP framework with inbuilt routing and middleware support.
- Host: GitHub
- URL: https://github.com/thecommieaxolotl/pyotr
- Owner: TheCommieAxolotl
- License: mit
- Created: 2023-07-31T09:58:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-23T10:22:26.000Z (about 1 year ago)
- Last Synced: 2024-11-14T20:02:23.743Z (1 day ago)
- Topics: express, http, https, javascript, middleware, nodejs, router, typescript
- Language: TypeScript
- Homepage:
- Size: 85 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
---
Want to test it out? Check out the [Hello World example](https://stackblitz.com/edit/pyotr?file=index.js)!
---
# Pyotr
A tiny (< 2kb gzipped) 0-dependency HTTP wrapper with inbuilt routing and middleware support.## Use
To instantiate a new Pyotr server, simply use `app`:
```ts
import http from "http";import { app } from "pyotr";
const server = app(3000); // you can specify a port for local development
const server = app(http.createServer()); // you can also use an existing server
```To add a route, use `app.attach` with a route provider:
```ts
import { app, route } from "pyotr";
import { html } from "pyotr/aura";const server = app(3000);
server.attach(route("/", () => html`
Hello, world!
`));
```If you want to use a whole directory as routes, instead of adding them one by one, you can use `app.use`:
```ts
import { resolve } from "node:path"
import { app } from "pyotr";const server = app(3000);
const useOptions = {
recursive: true, // whether to recursively attach directories
prettyUrls: true, // whether or not to use pretty URLs (e.g. /about instead of /about.html)
guessTypes: true, // guess the MIME type of files (e.g. text/css for .css files)
};server.use(resolve(process.cwd(), "./routes"), useOptions);
```### Route Handlers
Route handlers are functions that are called when a request is made to a route. They are passed a [`PyotrRequest`]("https://github.com/TheCommieAxolotl/pyotr/blob/main/src/router/route.ts#L6-L12") object and may return a subset of Response options.```ts
import { app, route } from "pyotr";const server = app(3000);
server.attach(route("/", (req) => {
const { request, method, path, query, params } = req;return {
type: string, // MIME type
content: string,
status: number, // HTTP status code
redirect: string, // redirect URL (should also set status to 302)
headers: Record
};
}));
```You can also update an existing route's handler by calling `route.update`:
```ts
const myRoute = route(...)server.attach(myRoute);
myRoute.update((req) => {
// ...
});
```