request
Access:
public

This is the request class. It is supplied as first parameter in a route handler and contains information about the request that was send by the client.

Examples

// this is an example of how to get the request class
cerus.router().route("/")
.then(req, res, next) {
  // the req parameter is the request class
});

Summary

Name
Description
Returns the request.accepts class.
Returns the request.agent class.
Sets for how long the socket can be kept alive.
Returns the body of the request.
Returns the request.bytes class.
Returns if the client is currently connecting.
Returns the connection header.
Returns the specified cookie.
Returns the request.cookies class.
Destroys the socket that received the request.
Returns a promise that is called on every event.
get
Fetches the specified data from the request.
Returns the hash part of the requested url.
Returns the specified header.
Returns the request.headers class.
Returns the hostname of the request.
is
Returns the matched content-type.
Returns the request.local class for this request.
Returns the method used for the request.
Returns the path of the request.
Returns the pathname of the request.
Returns the protocol used for the request.
Checks if the request was send using a proxy.
Returns if there is data available to read.
Returns the request.remote class for this request.
Checks if the connnection is secure.
Timeouts the request for the specified amount of time.
url
Returns the url used for the request.
Returns the http version sent by the client.
xhr
Checks if the request was send using XHR.

Functions

.accepts()
Access:
public
Summary:
Returns the request.accepts class.

With the class this function returns (request.accepts) you can check what the client accepts. This class is basically a wrapper for dougwilson's accepts module.

Returns

Type
Class
Description
The request.accepts class.
.agent()
Access:
public
Summary:
Returns the request.agent class.

This function returns the request.agent class. This class contains more information about the client by parsing the user-agent header. This is class is a wrapper of faisalman's ua-parser-js package.

Returns

Type
Class
Description
The request.agent class.
.alive(enable, delay)
Access:
public
Summary:
Sets for how long the socket can be kept alive.

With this function you can set for how long the socket can be kept alive. With the first parameter you can also set if keep alive is enabled. This needs to be set to true if you want the delay to work.

Parameters

Name
Type
Description
enable
Boolean
If keep alive may be enabled.
delay
Number
How long to keep the socket alive.
.body()
Access:
public
Summary:
Returns the body of the request.

This function returns the body of the request. This means it contains the data the client send with the request. Currently the only accepted body types are JSON and plain text. This function should only be called after the "end" event was fired. Otherwise it might be empty since the request wasn't finished yet.

Examples

// inside a route handling function
req.event()
.on("end", function() {
// use req.body() here
});
.bytes()
Access:
public
Summary:
Returns the request.bytes class.

This function will return the request.bytes class for this request. This class contains stats about the bytes that were read and written by the request.

Returns

Type
Class
Description
The request.bytes class.
.connecting()
Access:
public
Summary:
Returns if the client is currently connecting.

This function returns true if the client is currently connecting to the server. It is set to false when the client has succesfully connected.

Returns

Type
Boolean
Description
If the client is currently connecting.
.connection()
Access:
public
Summary:
Returns the connection header.

This function will return the connection header. This header contains the type of connection the client requested. For example, "close" means the client would like to close the connection.

Returns

Type
String
Description
The connection header sent by the client.
.cookies()
Access:
public
Summary:
Returns the request.cookies class.

This function will return the request.cookies class. With this class you can get the cookies that were sent in the request.

Returns

Type
Class
Description
The request.cookies class.
.destroy(error)
Access:
public
Summary:
Destroys the socket that received the request.

With this function you can destroy the socket that received the request. This makes the server unusable and should therefor only be used when there is an important error. You can also add an error. This error will be used as parameter in the error event.

Parameters

Name
Type
Description
error
String
The error you want to add. (optional)
.event()
Access:
public
Summary:
Returns a promise that is called on every event.

With this function you can listen for all the request events. It'll return a promise that will be called on a number of events.

Emits

Event
Description
data
This event will be called when there was data received from the client. It will have the data as first parameter.
end
This event will be called when all data was received and there is no data left. You should for this event until working with the request data.
close
This event will be called when the response was closed.
aborted
This event will be called when it was aborted by the socket.
error
This event is thrown when there was an error.
timeout
This event will be called when the client timed out.

Returns

Type
Promise
Description
This function will return a promise.
.get(key)
Access:
public
Summary:
Fetches the specified data from the request.

With this function you can get the specified data that was send by the request. If it was a POST request the data is currently just the parsed JSON. In the future this class will also accept different types of data. For the other methods the data is fetched from the query that is parsed from the url.

Parameters

Name
Type
Description
key
String
The key to specify which data to get.

Examples

// inside a route handling function and with "/home?data=example" as url
req.get("data");
// -> will return "example"

Returns

Type
String
Description
The specified data from the request.
.hash()
Access:
public
Summary:
Returns the hash part of the requested url.

This function will return the hash part of the url used in this request. The hash is the last bit of the url after the "#" (hashtag). For example, the hash of the url "/home#example" would be "example".

Returns

Type
String
Description
The hash part of the requested url.
.headers()
Access:
public
Summary:
Returns the request.headers class.

This function will return the request.headers class. With this class you can get the headers that were sent in the request.

Returns

Type
Class
Description
The request.headers class.
.hostname()
Aliases:
host
Access:
public
Summary:
Returns the hostname of the request.

This function is used to get the hostname of the request. Before just returns the Host header the proxy hostname is checked since it has more importance. If there is a proxy hostname it is first fixed before being returned. The fixes include removing brackets, etc.

Returns

Type
String
Description
The hostname of the request.
.is(types)
Aliases:
type
Access:
public
Summary:
Returns the matched content-type.

With function you can check the content-type of the request. The content-type is the header that is sent to inform what type of data the request data is. You can insert as many parameters as you want and the content-type will be returned from those parameters. If none of the paramters match the content-type false is returned. This also a wrapper for one of dougwilson's modules.

Parameters

Name
Type
Description
types
...String
The types to match.

Examples

// inside a route handling function and with "application/json" as content-type
req.is("html", "json");
// -> will return "json"

Returns

Type
String, Boolean
Description
Returns the matched content-type or false.
.local()
Access:
public
Summary:
Returns the request.local class for this request.

This function will return the request.local class for this request. This class contains information about the local address that was used to connect to the server.

Returns

Type
Class
Description
The request.local class for this request.
.method()
Access:
public
Summary:
Returns the method used for the request.

This function returns the method that was used for the request.

Examples

// inside a route handling function and with "GET" as method
req.method()
// -> will return "GET"

Returns

Type
String
Description
The method used for the request.
.path()
Access:
public
Summary:
Returns the path of the request.

This function will return the path for this request. The path is different from the href and url since it doesn't contain things like the hash and domain. For example, from the url "/home#test" the path is "/home". Please make sure that when you're not using the possible arguments in the url to use pathname instead.

Returns

Type
String
Description
The path of the request.
.pathname()
Access:
public
Summary:
Returns the pathname of the request.

This function will return the pathname for this request. The pathname is completely different from the path, since it doesn't contain the arguments that the path might also contain. For example pathname for the url "/home?test" is "/home", where .path() will return it fully.

Returns

Type
String
Description
The pathname of the request.
.protocol()
Access:
public
Summary:
Returns the protocol used for the request.

This functions returns the protocol that is used for the request. This can be one of two values: "http" or "https". "https" means that the request was secure.

Returns

Type
String
Description
The protocol used for the request.
.proxy()
Access:
public
Summary:
Checks if the request was send using a proxy.

This function will check if the request was send using a proxy. It does this by checking if the X-Forwarded-Proto header was set.

Returns

Type
Boolean
Description
If the request was send using a proxy.
.readable()
Access:
public
Summary:
Returns if there is data available to read.

This function returns if there is data readable from the request, meaning if there is data available to read.

Returns

Type
Boolean
Description
If there is data available to read.
.remote()
Access:
public
Summary:
Returns the request.remote class for this request.

This function will return the request.remote class for this request. This class contains information about the address of the user.

Returns

Type
Class
Description
The request.remote class for this request.
.secure()
Access:
public
Summary:
Checks if the connnection is secure.

With this function you can check if the connection is secure, meaning it is over "https".

Returns

Type
Boolean
Description
If the connection is secure.
.timeout(timeout)
Access:
public
Summary:
Timeouts the request for the specified amount of time.

Using this function you can timeout the request for a certain amount of time. By timing the connection out the request will have to wait for the specified amount of time. Since this happens asynchronous this function will return promise.

Parameters

Name
Type
Description
timeout
Number
How long to timeout the connection for.

Returns

Type
Promise
Description
This function returns a promise.
.url()
Aliases:
href
Access:
public
Summary:
Returns the url used for the request.

This function will return the url that was used for this request. This will be the full url the client used. Meaning that things like the hash are included.

Examples

// inside a route handling function and with "/home" as url
req.url()
// -> will return "/home"

Returns

Type
String
Description
The url used for the request.
.version()
Access:
public
Summary:
Returns the http version sent by the client.

This function will return the HTTP version sent by the client. This is often the same as the version the server is running on.

Returns

Type
String
Description
The http version sent by the client.
.xhr()
Access:
public
Summary:
Checks if the request was send using XHR.

This function is used to determine if the request was send using XHR, a.k.a. XMLHTTPRequest. It does this by checking if the X-Requested-With header matches "xmlhttprequest".

Returns

Type
Boolean
Description
If the request was send using XHR.