pyotr
v1.2.2
Published
A tiny (< 2kb) HTTP wrapper with inbuilt routing and middleware support.
Downloads
6
Maintainers
Readme
Want to test it out? Check out the Hello World example!
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
:
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:
import { app, route } from "pyotr";
import { html } from "pyotr/aura";
const server = app(3000);
server.attach(route("/", () => html`<h1>Hello, world!</h1>`));
If you want to use a whole directory as routes, instead of adding them one by one, you can use app.use
:
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
object and may return a subset of Response options.
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<string, string>
};
}));
You can also update an existing route's handler by calling route.update
:
const myRoute = route(...)
server.attach(myRoute);
myRoute.update((req) => {
// ...
});