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
npm install reqponse and start using the module:
'use strict';let http =reqponse = ;http;
$ = new Reqponse(request, response)
Constructor of Reqponse class returns a request-response object, so-called reqponse.
$.request and $.response
These properties just store original request and response objects, passed in constructor.
This property is a string, containing the method's name of the request in upper case.
This property is a number, a status of the further response (by default is equal 200).
This property is an object, including parsed URL of the request:
- original - it is the same as
- parsed - it is the same as
- pathname - it is the same as
- 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
This property is an instance of Header class (read below).
This property is an instance of Cookie class (read below).
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.
This method adds text to the buffer.
This method clears the buffer.
$.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.
This method adds to the buffer callstack of occured error. It is userful to catch errors in the browser.
This method does following:
- sets header
Set-Cookie, containing cookie from
- sets status for the response as
- sets headers for the response from
- writes buffer in the response;
- ends the response.
$.header = new Header(headers)
The constructor of the Header class gets
$.request.headers, parses and stores ones.
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.
This method sets the response-header
value. Case doesn't matter. Note that you don't need specify
Set-Cookie header (it is done automatically).
Returns an object, containing all headers specified for the response.
$.cookie = new Cookie(cookies)
The constructor of the Cookie class gets
Utils.splitIntoPairs($.request.headers.cookie, '; ') and stores ones.
This method returns the value of a request-cookie with
name. If required cookie doesn't exist in the request,
undefined is returned.
$.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
timeoutseconds (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
Returns cookie with
name as a string.
Returns an object with a single property
Set-Cookie, whose value contains all cookies for the response.
This method splits the
delim, then splits each substring by
=. Left part is a key, right part is a value of a resulting object.
This method wraps the
string for a beautiful display of debugging information.
It is an array, containing colors of debugging messages. By default the value is:
['#DD0011', '#F17217', '#DAA520', '#5BA51E', '#009C9F', '#1453FF', '#8A2BE2']