1.2.2 • Published 2 years ago

pyotr v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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) => {
    // ...
});
1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago