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

@leismore/all_handler

v3.0.2

Published

A Node.js and Express.js handler function for non-allowed-HTTP-methods.

Downloads

7

Readme

all_handler-function

A Node.js and Express.js handler function for rejecting non-allowed-HTTP-methods.

Donation

By me a coffee PayPal Donation

Installation

npm install @leismore/all_handler

Test

npm test

Example

import express = require('express');
import { LMResponse as Resp, LMResponseData as RespData } from '@leismore/response';
import { all_handler_generator            as generator,
         all_handler_LMErrorRes_generator as gen_response,
         ExpressRoutingHandler } from '@leismore/all_handler';
import { LMError } from '@leismore/lmerror';

const app  = express();
const port = 8080;

const ALLOWED = ['head', 'get', 'POST'];
const error405 = new LMError(
    { message:"HTTP 405: Method Not Allowed", code:"405" },
    gen_response(ALLOWED)
);

const all_handler:ExpressRoutingHandler = generator(ALLOWED, error405);

const get_handler:ExpressRoutingHandler = (req:express.Request, res:express.Response, next:express.NextFunction) => {
    const resp = new Resp(res);
    let data:RespData = {statusCode: '200', headers: {'Content-Type': 'application/json'}, body: {'result': 'OK'}};
    resp.send(data);
};

const post_handler:ExpressRoutingHandler = get_handler;

app.all('/', all_handler);
app.get ( '/', get_handler  );
app.post( '/', post_handler );

app.listen(port, () => {
  console.log(`@leismore/all_handler testing server, listening at http://localhost:${port}`);
});

API

Types

type ExpressRoutingHandler = (req:express.Request, res:express.Response, next:express.NextFunction) => void;

type LMErrorRes = {
  readonly statusCode:  string,                         // HTTP response status code
           headers?:   {readonly [key:string]: string}, // HTTP headers
           body?:       any                             // HTTP body
};

Function: all_handler_generator

/**
 * Generate a non-allowed-HTTP-methods handler function.
 * @param  allowed HTTP methods names
 * @param  error   LMError (or sub-class) instance
 */
function all_handler_generator(allowed: string[], error: LMError):ExpressRoutingHandler

Function: all_handler

/**
 * Test HTTP request methods, if allowed, pass to its handlers
 * else
 *   pass a LMError to the next error handler.
 */
function all_handler(req:express.Request, res:express.Response, next:express.NextFunction): void

Function: all_handler_LMErrorRes_generator

/**
 * Generate a LMErrorRes object for LMError
 * @param  allowed  HTTP methods names
 */
function all_handler_LMErrorRes_generator(allowed: string[]):LMErrorRes

License

GNU Affero General Public License v3.0

Authors