0.1.0 • Published 4 years ago

@whi/http_utils v0.1.0

Weekly downloads
57
License
ISC
Repository
github
Last release
4 years ago

npm.io

HTTP Utilities

This module is intended to extend the built-in HTTP server and provide a user friendly HTTP client wrapper around node-fetch.

npm.io npm.io npm.io

Server

Create a server that defaults to static assets but can be configured to return dynamic responses.

const { server } = require('@whi/http');

const http_server = new server();
const settings = { version: "1.0.0" };

http_server.serve_local_assets( "./public/", function ( req_path ) {
    if ( req_path === "/settings" ) {
        this.contentType("application/json");
        return JSON.stringify( settings );
    }
});

http_server.listen( 8080 );

Client

HTTP client using node-fetch.

const { client } = require('@whi/http');

const fetch = client.create(`http://localhost:8080`);
const resp = await fetch.get("/settings");

resp.version === "1.0.0";