Gathering detailed insights and metrics for node-req
Gathering detailed insights and metrics for node-req
Gathering detailed insights and metrics for node-req
Gathering detailed insights and metrics for node-req
npm install node-req
Typescript
Module System
Node Version
NPM Version
94.9
Supply Chain
97.7
Quality
77.7
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A facade over Node.js HTTP
req
object with no side-effects.
node-req
is an i/o module for parsing and returning values out of HTTP request object using helper methods.
1var http = require('http') 2var nodeReq = require('node-req') 3 4http.createServer(function (req, res) { 5 6 // get query string from req 7 var query = nodeReq.get(req) 8 9}).listen(3000) 10
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.
Object
String
Object
String
Boolean
Boolean
String
Array
String
Boolean
Array
Boolean
Boolean
String
String
String
String
String
Array
String
Array
String
Array
String
Array
Boolean
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
1const queryString = nodeReq.get(req)
String
Returns the exact copy of request.method
. Defined
here
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
Example
1const method = nodeReq.method(req)
Object
Returns an object of headers for a given request.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
Example
1const headers = nodeReq.headers(req)
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
1const authHeader = nodeReq.header(req, 'Authorization')
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
1if (nodeReq.fresh(req, res)) { 2 res.writeHead(304) 3}
Boolean
This method is the opposite of the nodeReq.fresh
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
res | http.ServerResponse |
Example
1if (!nodeReq.stale(req, res)) { 2 res.writeHead(304) 3}
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
1nodeReq.ip(req, '127.0.0.1') 2nodeReq.ip(req, ['::1/128', 'fe80::/10'])
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'])
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)
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)
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
1const subdomains = nodeReq.subdomains(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
1if (nodeReq.ajax(req)) { 2 res.writeHead(200, {"Content-type": "application/json"}) 3} else { 4 res.writeHead(200, {"Content-type": "text/html"}) 5}
Boolean
Tells whether request has X-Pjax header or not.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
Example
1if (nodeReq.pjax(req)) { 2 // return partial content 3} else { 4 // full page refresh 5}
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
1const hostname = nodeReq.hostname(request)
String
Returns request url after removing the query string.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
Example
1const url = nodeReq.url(request)
String
Returns the untouched url.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
Example
1const url = nodeReq.originalUrl(request)
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
1// req.headers.content-type = 'application/json' 2 3nodeReq.is(req, ['json']) // json 4nodeReq.is(req, ['json', 'html']) // json 5nodeReq.is(req, ['application/*']) // application/json 6 7nodeReq.is(req, ['html']) // '<empty string>'
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
1const type = nodeReq.accepts(req, ['json', 'html']) 2 3switch(type) { 4 case 'json': 5 res.setHeader('Content-Type', 'application/json') 6 res.write('{"hello":"world!"}') 7 break 8 9 case 'html': 10 res.setHeader('Content-Type', 'text/html') 11 res.write('<b>hello, world!</b>') 12 break 13 14 default: 15 res.setHeader('Content-Type', 'text/plain') 16 res.write('hello, world!') 17}
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 |
String
Returns one of the most preferrable language.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
accepted | Array |
Array
Returns list of all accepted languages from most to least preferred one.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
String
Returns the best maching encoding
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
accepted | Array |
Array
Returns list of all encodings from most to least preferred one.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
String
Returns the best maching charset based upon
Accept-Charset
header.
Kind: inner method of Request
Param | Type |
---|---|
req | http.IncomingMessage |
accepted | Array |
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 |
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
1if (nodeReq.hasBody(request)) { 2 // use body parser 3}
No vulnerabilities found.
No security vulnerabilities found.