GithubHelp home page GithubHelp logo

bhh-node's Introduction

Better HTTP Handler (BHH) for node.js

Quickstart

const BHH = require("bhh-node");

var server = new BHH();
server.maxRequestBodySize = 500; //Bytes

server.register_handler(
    "/test/{test_arg}",
    async (request, response, argv)=>{
        console.log(`Received argument ${argv.test_arg}`)
        response.writeHead(
            200,
            {
                "Content-Type": "application/json"
            }
        );
        response.end(JSON.stringify({
            result: "success",
            args: argv
        }));
    }
);

server.register_handler(
    "/test",
    async (request, response, argv) => {
        try{
            var data = JSON.parse(request.body);
        } catch(e){
            server.errorHandler(request, response, {error:400, message:e.message}, 400);
            return;
        }
        response.writeHead(
            200,
            {
                "Content-Type": "application/json"
            }
        )
        response.end(JSON.stringify({
            result: "success",
            data: data
        }));
    },
    "POST"
)

console.log("Listening on port 8080");
server.listen(8080);

Reference

new BHH()

Constructor of the BHH class. No argument is needed.

BHH.listen()

The same as http.Server.listen()

BHH.register_handler(path, handler, method?, optional_trail_slash?)

Argument Type Description
path string URI format. Using the {} bracket means that the section is a variable. {} is suggested to be used between two slashes.
handler async function (IncomingMessage, ServerResponse, Object) The handler. Object in 3rd is the named variables sources from the URI.
method string Request method. Default: GET
optional_trail_slash boolean If the URI have optional / at the end. Default: true

Property defaultPages

Type Array(string)
Default value ["index.html", "index.htm"]

A list of filenames that can be served as static file when requesting a directory.

Property errorHandler

Type function (IncomingMessage, ServerResponse, Object, int)
Default value BHH.prototype._error

Normalized error handling function.

Third argument is some basic information about the error.

Forth argument is the suggested HTTP status code to be sent.

When server built-in error is triggered (e.g. static file not found), this handler will be triggered like server.errorHandler(request, response, {error: 404, message:"Not found"}, 404)

Property maxRequestBodySize

Type number
Default value 1000

The maximum allowed size for request body.

Property staticRoot

Type string
Default value ""

Static file will be searched when no handler available for a path. staticRoot is a path that contains all static files.

bhh-node's People

Contributors

jacky9813 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.