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

reqponse

v0.0.4

Published

A small module to construct request-response objects, contained the most necessary data like headers, cookies, form fields and other...

Downloads

3

Readme

reqponse

This module provides the class to combine request and response objects into a single entity - reqponse. On the one hand it parses request and makes the access to form data, url components, cookie and headers really easy. On the other hand reqponse contains some methods to make the most often actions with response.

How to install

Just type npm install reqponse and start using the module:

'use strict';
let http = require('http'),
    reqponse = require('reqponse');

http.createServer((request, response) => {
    let $ = new reqponse.Reqponse(request, response);
    $.parse(error => {
        if (error) {
            $.status = 500;
            $.error(error).send();
        } else {
            /* Now you can use the instance of Reqponse class */
            let headerName = $.get.headerName || 'cookie',
                headerValue = $.header.get(headerName);

            $.echo('name: ' + headerName + '\n');
            $.echo('value: ' + headerValue);

            $.header.set('Content-Type', 'text/plain');
            $.send();
        }
    });
}).listen(80);

Reqponse

$ = new Reqponse(request, response)

Constructor of Reqponse class returns a request-response object, so-called reqponse.

returns $

$.request and $.response

These properties just store original request and response objects, passed in constructor.

$.method

This property is a string, containing the method's name of the request in upper case.

$.status

This property is a number, a status of the further response (by default is equal 200).

$.url

This property is an object, including parsed URL of the request:

  • original - it is the same as $.request.url;
  • parsed - it is the same as require('url').parse($.request.url);
  • pathname - it is the same as $.url.parsed.pathname;
  • splitted - it is the pathname, splitted by /.

$.get or $.post and $.files

To get access to form data you need to call $.parse method. It stores all fields in the property, named according with $.method. All files are saved to $.files variable.

$.header

This property is an instance of Header class (read below).

$.cookie

This property is an instance of Cookie class (read below).

$.parse(callback, config)

Parses form data and stores it in the reqponse object. Formidable module is used under the hood, config (optional) is passed into one. Callback gets only one parameter - occured error or null.

returns $

$.echo(text)

This method adds text to the buffer.

returns $

$.clear()

This method clears the buffer.

returns $

$.debug(anything1, anything2, ...)

This method adds to the buffer debugging information about each argument (util.inspect uses). It is useful to debug your application in the browser.

returns $

$.error(error)

This method adds to the buffer callstack of occured error. It is userful to catch errors in the browser.

returns $

$.send()

This method does following:

  • sets header Set-Cookie, containing cookie from $.Cookie;
  • sets status for the response as $.status;
  • sets headers for the response from $.Header;
  • writes buffer in the response;
  • ends the response.

returns $

Header

$.header = new Header(headers)

The constructor of the Header class gets $.request.headers, parses and stores ones.

returns $.header

$.header.get(name)

This method returns the value of a request-header with name. Remember, that name is lower case. If required header doesn't exist in the request, undefined is returned.

returns string

$.header.set(name, value)

This method sets the response-header name to value. Case doesn't matter. Note that you don't need specify Set-Cookie header (it is done automatically).

returns $.header

$.header.toHeader()

Returns an object, containing all headers specified for the response.

returns object

Cookie

$.cookie = new Cookie(cookies)

The constructor of the Cookie class gets Utils.splitIntoPairs($.request.headers.cookie, '; ') and stores ones.

returns $.cookie

$.cookie.get(name)

This method returns the value of a request-cookie with name. If required cookie doesn't exist in the request, undefined is returned.

returns string

$.cookie.set(name, value, timeout, path, domain, secure, httponly)

This method sets the response-cookie:

  • name (required) is a cookie's name.
  • value (required) is a cookie's value.
  • timeout (optional) is a cookie's lifetime. Cookie expires after timeout seconds (by default cookie lives only within a current session).
  • path (optional) is a cookie's path. By default it is equal /.
  • domain (optional) is a cookie's domain.
  • secure (optional) is a boolean flag, which says to set cookie only if HTTPS uses. It is false by default.
  • httponly (optional) is a boolean flag, which denies use cookie from javascript. It is false by default.

returns $.cookie

$.cookie.toString(name)

Returns cookie with name as a string.

returns string

$.cookie.toHeader()

Returns an object with a single property Set-Cookie, whose value contains all cookies for the response.

returns object

Utils

splitIntoPairs(string, delim)

This method splits the string by delim, then splits each substring by =. Left part is a key, right part is a value of a resulting object.

returns object

wrapDebugInfo(string)

This method wraps the string for a beautiful display of debugging information.

returns string

Colors

It is an array, containing colors of debugging messages. By default the value is: ['#DD0011', '#F17217', '#DAA520', '#5BA51E', '#009C9F', '#1453FF', '#8A2BE2']