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

node-req

v2.1.2

Published

I/O parser for nodejs http request

Downloads

55,138

Readme

Node Req

A facade over Node.js HTTP req object with no side-effects.

NPM Version Build Status Appveyor Coveralls

node-req is an i/o module for parsing and returning values out of HTTP request object using helper methods.

See also

  1. node-res
  2. node-cookie

Http Server

var http = require('http')
var nodeReq = require('node-req')

http.createServer(function (req, res) {

  // get query string from req
  var query = nodeReq.get(req)

}).listen(3000)

Yes, that's all, node-req makes no assumption on how to add routes or handle HTTP requests. All it does it parse request object and return values out of it.

API

get(req, [options]) ⇒ Object

Parses query string from url an returns an object.

Kind: inner method of Request

| Param | Type | Description | | --- | --- | --- | | req | http.IncomingMessage | | | [options] | Object | Options are passed to https://www.npmjs.com/package/qs |

Example

const queryString = nodeReq.get(req)

method(req) ⇒ String

Returns the exact copy of request.method. Defined here

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

const method = nodeReq.method(req)

headers(req) ⇒ Object

Returns an object of headers for a given request.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

const headers = nodeReq.headers(req)

header(req, key) ⇒ String

Returns header value for a given key. Also it will handle the inconsistencies between referer and referrer header.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | key | String |

Example

const authHeader = nodeReq.header(req, 'Authorization')

fresh(req, res) ⇒ Boolean

Returns the freshness of a response inside the client cache. If client cache has the latest response, this method will return true, otherwise it will return false.

Also when HTTP header Cache-Control: no-cache is present this method will return false everytime.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | res | http.ServerResponse |

Example

if (nodeReq.fresh(req, res)) {
   res.writeHead(304)
}

stale(req, res) ⇒ Boolean

This method is the opposite of the nodeReq.fresh

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | res | http.ServerResponse |

Example

if (!nodeReq.stale(req, res)) {
   res.writeHead(304)
}

ip(req, [trust]) ⇒ String

Returns the most trusted ip address for the HTTP request. It will handle the use cases where your server is behind a proxy.

Make sure to check proxy-addr for the available options for trust.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | [trust] | Mixed |

Example

nodeReq.ip(req, '127.0.0.1')
nodeReq.ip(req, ['::1/128', 'fe80::/10'])

ips(req, [trust]) ⇒ Array

Returns list of all remote addresses ordered with most trusted on the top of the list.

Make sure to check proxy-addr for the available options for trust.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | [trust] | Mixed |

Example

nodeReq.ips(req, '127.0.0.1')
nodeReq.ips(req, ['::1/128', 'fe80::/10'])

protocol(req, [trust]) ⇒ String

Returns request protocol based upon encrypted connection or X-Forwaded-Proto header.

Make sure to check proxy-addr for the available options for trust.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | [trust] | Mixed |

Example

const protocol = nodeReq.protocol(req)

secure(req) ⇒ Boolean

Looks for request protocol to check for https existence or returns false.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

const isHttps = nodeReq.secure(req)

subdomains(req, [trust], [offset]) ⇒ Array

Returns the request subdomains as an array. Also it will make sure to exclude www from the subdomains list.

Make sure to check proxy-addr for the available options for trust.

Kind: inner method of Request

| Param | Type | Default | Description | | --- | --- | --- | --- | | req | http.IncomingMessage | | | | [trust] | Mixed | | | | [offset] | Number | 2 | subdomain offset |

Example

const subdomains = nodeReq.subdomains(req)

ajax(req) ⇒ Boolean

Determines whether request is an ajax request or not, based on X-Requested-With header.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

if (nodeReq.ajax(req)) {
   res.writeHead(200, {"Content-type": "application/json"})
} else {
   res.writeHead(200, {"Content-type": "text/html"})
}

pjax(req) ⇒ Boolean

Tells whether request has X-Pjax header or not.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

if (nodeReq.pjax(req)) {
   // return partial content
} else {
   // full page refresh
}

hostname(req, [trust]) ⇒ String

Returns the hostname of HTTP request.

Make sure to check proxy-addr for the available options for trust.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | [trust] | Mixed |

Example

const hostname = nodeReq.hostname(request)

url(req) ⇒ String

Returns request url after removing the query string.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

const url = nodeReq.url(request)

originalUrl(req) ⇒ String

Returns the untouched url.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

const url = nodeReq.originalUrl(request)

is(req, keys) ⇒ String

Tells whether request accept content of a given type or not (based on Content-type) header.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | keys | Mixed |

Example

// req.headers.content-type = 'application/json'

nodeReq.is(req, ['json']) // json
nodeReq.is(req, ['json', 'html']) // json
nodeReq.is(req, ['application/*']) // application/json

nodeReq.is(req, ['html']) // '<empty string>'

accepts(req, keys) ⇒ String

Return the best possible response accepted by the client. This is based on the Accept header. Learn more about it

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | keys | Mixed |

Example

const type = nodeReq.accepts(req, ['json', 'html'])

switch(type) {
 case 'json':
   res.setHeader('Content-Type', 'application/json')
   res.write('{"hello":"world!"}')
   break

 case 'html':
   res.setHeader('Content-Type', 'text/html')
   res.write('<b>hello, world!</b>')
   break

 default:
   res.setHeader('Content-Type', 'text/plain')
   res.write('hello, world!')
}

types(req) ⇒ Array

This method is similar to {{#crossLink "Request/accepts"}}{{/crossLink}}, instead it will return an array of types from most to least preferred one.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

language(req, accepted) ⇒ String

Returns one of the most preferrable language.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | accepted | Array |

languages(req) ⇒ Array

Returns list of all accepted languages from most to least preferred one.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

encoding(req, accepted) ⇒ String

Returns the best maching encoding

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | accepted | Array |

encodings(req) ⇒ Array

Returns list of all encodings from most to least preferred one.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

charset(req, accepted) ⇒ String

Returns the best maching charset based upon Accept-Charset header.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage | | accepted | Array |

charsets(req) ⇒ Array

Returns a list of all charsets from most to least preferred one based upon Accept-Charset header.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

hasBody(req) ⇒ Boolean

Tells whether request has body or not to be read by any body parser.

Kind: inner method of Request

| Param | Type | | --- | --- | | req | http.IncomingMessage |

Example

if (nodeReq.hasBody(request)) {
   // use body parser
}