Coder Social home page Coder Social logo

http-to-fetch's Introduction

http-to-fetch

Very simple handler for converting node:http's IncomingMessage object into a Fetch API Request

Usage

npm install http-to-fetch

After starting a server using node:http, listen for requests and convert the objects. Example:

// (typescript)
import http from "node:http";

http.createServer((request, response) => {
    // create request and return 200 response
    const req = new Request();
    req.translate(request, response);

    // the HTTPResponse object will automatically close the request!
    new HTTPResponse(
        `Hi mom!`, // body
        {
            // options
            status: 200,
            headers: new Headers({
                "Content-Type": "text/plain",
            }),
        },
        req // request
    );
}).listen(8080);

Usage (with PluginAPI)

This library also includes a "Plugin API" that makes the whole process much more like the the native web Fetch API.

Using a basic loader (or the one from /example), just listen and handle the request.

// (typescript)
import loader from "./loader.js";

// start server
loader.start(8080);

// bind endpoint
loader.bind({
    endpoint: "/", // serve everything on the root endpoint (/)
    fetch: (Request, Response) => {
        return new Response(
            "Hello, world!", // body
            {
                // options
                status: 200,
                headers: new loader.Headers({
                    "Content-Type": "text/plain",
                    "Hi-Mom": "It works!",
                }),
            }
        );
    },
});

This loader uses the Plugin.registerEndpoint to register an endpoint for the endpoint you defined.

// (typescript)
// from: loader.ts
// ...
export function bindEndpoint(params: { endpoint: string; fetch: fetch }) {
    // create plugin client
    const pluginClient = new Plugin();

    // update plugin client bindings
    pluginClient.bindOnRegEndpoint((endpoint: string, fetch: fetch) => {
        endpointList[endpoint] = fetch;
        endpointList[`${endpoint}/`] = fetch; // account for trailing slash
    });

    // bind endpoint
    pluginClient.registerEndpoint(params.endpoint, params.fetch);
}
// ...

loader.bind just calls loader.bindEndpoint

// (typescript)
// from: loader.ts
// ...
export default {
    bind: bindEndpoint,
    // ...

It is recommended that you just use the example loader and extend it.

http-to-fetch's People

Contributors

0aoq 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.