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

livesey-routing

v1.0.6

Published

A simple router written in Node.js

Downloads

2

Readme

livesey-routing

livesey-routing is a lightweight HTTP router for Node.js that simplifies handling requests and responses. It provides tools to manage HTTP methods, URL parameters, request bodies, and custom routes.

Table of Contents

Getting Started

Installation

You can install the module via npm:

npm install livesey-routing

Usage

Module supports both ECMAScript and CommonJS:

To use the module in your project(ESM):

import http from 'node:http';
import { Router, RouteBuilder } from 'livesey-routing';

const router = new RouteBuilder()
  .get('/hello/:name', (req, res) => {
    const name = req.params.name;
    res.json({ message: `Hello, ${name}!` });
  })
  .post('/data', async (req, res) => {
    const data = await req.parseBody();
    res.json({ received: data });
  })
  .build();

(async () => {
  const server = http.createServer((req, res) => {
    router.handleRequest(req, res);
  });

  const port = 3000;
  server.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
  });
})();

To use the module in your project(CJS):

const http = require('node:http');
const { Router, RouteBuilder } = require('livesey-routing');

const router = new RouteBuilder()
  .get('/hello/:name', (req, res) => {
    const name = req.params.name;
    res.json({ message: `Hello, ${name}!` });
  })
  .post('/data', async (req, res) => {
    const data = await req.parseBody();
    res.json({ received: data });
  })
  .build();

(async () => {
  const server = http.createServer((req, res) => {
    router.handleRequest(req, res);
  });

  const port = 3000;
  server.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
  });
})();

Features

Request Class

The Request class provides methods to handle and parse incoming HTTP requests, including access to URL parameters, headers, and the request body.

import { Request } from 'livesey-routing';

const request = new Request(req);

// Access request method
const method = request.method;

// Access URL parameters
const params = request.extractParams('/hello/:name');

// Access request body
const body = await request.parseBody();

Response Class

The Response class simplifies sending responses to the client.

import { Response } from 'livesey-routing';

const response = new Response(res);

// Send a JSON response
response.json({ message: 'Success' });

// Send a plain text response
response.send('Hello, world!');

// Set status code
response.status(404).send('Not Found');

Routing

Routing allows you to define custom routes and their associated handlers using the RouteBuilder class.

import { RouteBuilder } from 'livesey-routing';

const router = new RouteBuilder()
  .get('/user/:id', (req, res) => {
    const userId = req.params.id;
    res.json({ userId });
  })
  .post('/submit', async (req, res) => {
    const data = await req.parseBody();
    res.json({ received: data });
  })
  .put('/update/:id', async (req, res) => {
    const userId = req.params.id;
    res.json({ message: `Updated user ${userId}` });
  })
  .delete('/delete/:id', (req, res) => {
    const userId = req.params.id;
    res.json({ message: `Deleted user ${userId}` });
  })
  .build();

Middleware and Nested Routers

You can use middleware and nested routers to structure your application routes more effectively.

import { Router, RouteBuilder } from 'livesey-routing';

const apiRouter = new RouteBuilder()
  .get('/search/:query', (req, res) => {
    const query = req.params.query;
    res.json({ query });
  })
  .build();

const appRouter = Router.use('/api', apiRouter);

const server = http.createServer((req, res) => {
  appRouter.handleRequest(req, res);
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss any changes.

License

This project is licensed under the MIT License.s

Contact

For any questions or inquiries, please contact [email protected].