npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pyotr

v1.2.2

Published

A tiny (< 2kb) HTTP wrapper with inbuilt routing and middleware support.

Downloads

36

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