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

unrepress

v1.0.4

Published

express but shittier. unrepress because just repress was taken by a guy who made a package with one line of code 8 years ago.

Downloads

8

Readme

unrepress

A Demo app

The library exposes:

route: a function you can use to add callback to a route and method combo

use: a function you can use to add middleware like cors

createServer: a function that creates the server

simple example

const { createServer, route, use } = require("unrepress");
const server = createServer();

route("/", "GET", (req, res) => {
  res.send("get /");
});

// Start server
server.listen(3000, () => {
  console.log("listening on port 3000");
});

routes

parameterized routes

route("/users/:userId/posts/:postId", "GET", (req, res) => {
  const { userId, postId } = req.params;

  const user = users.find((u) => u.id === +userId);

  if (user) {
    const post = user.posts.find((p) => p.id === +postId);
    if (post) {
      res.json(post);
    } else {
      res.status(404).send("Post not found");
    }
  } else {
    res.status(404).send("User not found");
  }
});

more ways for errors handling

// error routes
route("/error1", "GET", (req, res) => {
  throw 418;
});

route("/error2", "GET", (req, res) => {
  res.error(418, "It's teatime !");
});

middleware

const cors = require("cors");

use(cors());

custom middleware

const middleware1 = (req, res, next) => {
  process.stdout.write("mw1👉");
  next();
};
const middleware2 = (req, res, next) => {
  process.stdout.write("mw2👉 ");
  next();
};
const logger = (req, res, next) => {
  console.log(`${req.method} ${req.url} - ${Date.now()}`);
  next();
};
use([middleware1, middleware2]);
use(logger);

adding middleware for certain routes only

const removePosts = (req, res, next) => {
  const modifiedUsers = users.map((user) => {
    const { posts, ...rest } = user;
    return rest;
  });

  // persist state/info from one middleware to the next
  res.locals.users = modifiedUsers;
  req.users = modifiedUsers;

  next();
};

route("/users", "GET", removePosts, (req, res) => {
  // res.json(req.users);
  res.json(res.locals.users);
});

other usefull stuff the lib exposes

IncomingMessage: the Custom Incoming Message class the server uses. you can extend it and pass it to the options you can pass in the createServer function

ServerResponse: the Custom Server Response class

routes: a map of routes that maps from a string (the route you define using route) to an object example: { GET: RoutMethodInfo object, POST: {[], () => {}}}

routeCaller: a function that's called when a request event fires

middleware, an array of middleware functions

middleWareCaller, a function that calls the functions in the global middleware and local middleware array. takes req, res and a RouteMethodInfo object

paramRouteResolver: a function that converst a url with params like /users/12 to a url defined in the routes like /users/:user_id

RouteMethodInfo: a class that has two parameters, middleware: array of functions, and handler which is the route and method combo callback function

getRouteFromURL: a function that get's the route from the URL object. takes req.url

advanced middleware example using the other stuff

use((req, res, next) => {
  let route = getRouteFromURL(req.url);
  if (!routes.has(paramRouteResolver(getRouteFromURL(req.url)).paramRoute)) {
    res.error(400, `🤷‍♀️ ${route} not found`);
    return;
  }
  next();
});