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

http-box

v1.1.1

Published

Tiny, lightweight and modular http server with express-like routers and structure

Downloads

5

Readme

HTTP-box

HTTP-box is a lightweight http Node.js JavaScript library, inspired from Express.js ans its router system.

You can use it to easily setup a modular http server.

Installation

Regular installation

You can install HTTP-box using npm :

npm install http-box

Build from source

If you want to build HTTP-box yourself, follow these steps :

  • Clone this repository : git clone https://github.com/Ptitet/http-box.git
  • Go to the root directory : cd http-box
  • Install all the dependencies : npm install
  • Build the library : npm run build This library will be available in dist/lib/. The entry point of the library is lib.js.

Documentation

For more details on the different apis, check the documentation.

Usage examples

Simple server :

import { HTTPServer, RequestStatus } from 'http-box'; // or the path to lib/lib.js
const port = 3000;
const server = new HTTPServer({ port }); // create the server

server.get('/', (req, res) => {
    res.send('Welcome to the root !');
    return RequestStatus.Done; // tell the server the request has been handled
});

server.post('/echo', (req, res) => {
    let { body } = req;
    res.send(body); // send the request's body back
    return RequestStatus.Done;
});

server.start(() => console.log(`Server open at http://localhost:${port}`));

Here, a new server is created with the class HTTPServer. Then, with the <HTTPServer>.get() and <HTTPServer>.post() methods, two handlers are created, one at the root path / and an other at /echo. Finally, the server is started with <HTTPServer>.start().

Using routers :

import { HTTPServer, Router, RequestStatus } from 'http-box';
const port = 3000;
const server = new HTTPServer({ port });

const apiRouter = new Router;

apiRouter.use('/', (req, res) => {
    if (isAuthValid(req)) {
        req.data.authenticated = true;
        return RequestStatus.Next; // go to the next handler
    } else {
        res.status(401);
        res.end('Bad auth');
        return RequestStatus.Done;
    }
});

apiRouter.get('/timestamp', (req, res) => {
    res.send(Date.now().toString());
    return RequestStatus.Done;
});

server.use('/api', apiRouter); // mount the router on the path /api

server.start(() => console.log(`Server open at http://localhost:${port}`));

The apiRouter is created with the class Router. The method <Router>.use() add a handler which triggers on any request at the given path, here the root / of the router. A handler can be a callback function, or even an entire router : the line server.use('/api', apiRouter) mount the api router at the route /api of the server. So we can access the timestamp route at http://localhost:3000/api/timestamp.