New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

http4js

Package Overview
Dependencies
Maintainers
1
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http4js

A lightweight HTTP toolkit

  • 1.6.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
737
increased by47.11%
Maintainers
1
Weekly downloads
 
Created
Source

http4js

A simple http library for typescript

*** read the docs ***

Using in your project

To install:

npm install --save http4js
#or
yarn add http4js

Example

An example server and client


//define our server routes and start on port 3000
//GET to any path returns `GET to {PATH} with req headers {HEADERS}`
const routing = routes(Method.GET, ".*", (req: Request) => {
    const html = `<h1>${req.method} to ${req.uri.path()} with req headers ${Object.keys(req.headers)}</h1>`;
    return Promise.resolve(new Response(Status.OK, html));
})

//add csrf token header to every request and vary gzip to every response
const headerFilter = (handler: HttpHandler) => {
    return (req: Request) => {
        return handler(req.withHeader(Headers.X_CSRF_TOKEN, Math.random()))
            .then(response => response.withHeader(Headers.VARY, "gzip"));
    }
};

routing.withFilter(headerFilter)
    .asServer()
    .start();

//make an http request to our server and log the response
HttpClient(
    new Request(Method.GET, "http://localhost:3000/any/path")
).then(response => {
    console.log(response);
    console.log(response.bodyString());
});

/*
//response
Response {
    headers:
    { vary: 'gzip',
        date: 'Sun, 08 Apr 2018 08:26:20 GMT',
        connection: 'close',
        'transfer-encoding': 'chunked' },
    body:
        Body {
        bytes: <Buffer 3c 68 31 3e 47 45 54 20 74 6f 20 2f 61 6e 79 2f 70 61 74 68 20 77 69 74 68 20 72 65 71 20 68 65 61 64 65 72 73 20 68 6f 73 74 2c 63 6f 6e 6e 65 63 74 ... > },
    status: 200 }
    
//response.bodyString()
<h1>GET to /any/path with req headers host,connection,x-csrf-token</h1>

 */

Contributing

I'd be very happy if you'd like to contribute :)

To run:

git clone git@github.com:TomShacham/http4js.git  
cd http4js
yarn #or npm install
yarn start #or tsc; node index.js
To test:
yarn
yarn test

History and Design

http4js is a port of http4k.

The concept is called Server as a Function (SaaF).

Keywords

FAQs

Package last updated on 20 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc