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

froq-http

v0.0.23

Published

Integration Testing environment orchestration made easy for HTTP.

Downloads

13

Readme

froq-http

npm node

Travis branch Coveralls github

license GitHub tag GitHub issues GitHub last commit GitHub top language GitHub code size in bytes

froq-http is not a framework for production, but for testing, creating mocks for integration tests on the fly. At current, https implemention is missing.

Usage

npm install froq-http

We use npm package debug. To make me verbose use DEBUG=froq-http.

Create an HTTP Server

import http from 'froq-http';
const server = await http();

Simple Route

    
server
    .rest `/news`
    .type('json')
    .respond({test: true})
;

const result = await fetch(server.url('/news'));
const json = await result.json();
// {test: true}

await server.stop();

Route with templates

server
    .rest `/news/${'id'}`
    .respond(({result}) => {

        // result[0]: '12345'
        // result.id: '12345'

        return http.resp({
            type: 'json',
            body: result[0]
        });
    })
;

const result = await fetch(server.url('/news/12345'));
const json = await result.json();
// '12345'

await server.stop();

Proxy Server

// parallel
const [server1, server2] = await Promise.all([
    http('server1'),
    http('server2')
]);

server2
    .rest `/api/docs`
    .type('json')
    .respond([
        'doc1',
        'doc2'
    ]);

server1
    .rest `/index.html`
    .type('html')
    .respond('<html>...</html>')

    .rest `/${'*'}`
    .proxy(server2)
;


{
    // direct server1
    const result = await fetch(server1.url('/index.html'));
    const text = await result.text();
    // '<html>...</html>'
}

{
    // indirect server2 via server1
    const result = await fetch(server1.url('/api/docs'));
    const json = await result.json();
    // ['doc1', 'doc2']
}

await Promise.all([server1.stop(), server2.stop()]);

Route with Error

server
    .rest `/api/error`
    .respond(() => http.resp({
        status: 404,
        type: 'json',
        body: 'Not Found'
    }))
;

const result = await fetch(server.url('/api/error'));
// result.status: 404
// await result.json(): 'Not Found'

await server.stop();

Simple Server implementation

server
    .rest `/api/endpoint`
    .type('json')
    .respond(({body}) => ({
        a: body.a + 1,
        b: body.b + 'b'
    }))
;

const result = await fetch(server.url('/api/endpoint'), {
    method: 'POST',
    headers: {
        'content-type': 'application/json'
    },
    body: JSON.stringify({
        a: 1,
        b: 'b'
    })
});

const json = await result.json();
// {a: 2, b: 'bb'}

await server.stop();

Response types

server
    .type('json')

    .rest `/api/json`
    .respond({
        json: true
    })

    .rest `/api/html`
    .type('text/html')
    .respond('<p>Html</p>')

    .rest `/api/txt`
    .respond(http.resp({type: 'txt', body: 'This is a text.'}))
;

{
    // json
    const result = await fetch(server.url('/api/json'));
    // result.headers['content-type']: 'application/json'

    const json = await result.json();
    // {json: true}
}

{
    // html
    const result = await fetch(server.url('/api/html'));
    // result.headers['content-type']: 'text/html'
    
    const text = await result.text();
    // '<p>Html</p>'
    
    
}

{
    // text
    const result = await fetch(server.url('/api/txt'));
    // result.headers['content-type']: 'text/plain'

    const text = await result.text();
    // 'This is a text.'
}

await server.stop();

Micro Login Server

server
    .rest `/login`
    .type('json')
    .respond(({body}) => {
        if (body.username === 'user' && body.password === 'pass') {
            return {
                login: true
            };
        }

        return http.resp({
            status: 401,
            body: {
                login: false
            }
        });
    })
;

{
    // successful login
    const response = await fetch(server.url('/login'), {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify({
            username: 'user',
            password: 'pass'
        })
    });

    const json = await response.json();
    // {login: true}
}

{
    // login failed
    const response = await fetch(server.url('/login'), {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify({
            username: 'invalid_user',
            password: 'invalid_pass'
        })
    });

    const json = await response.json();
    // {login: false}
}


await server.stop();