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.
// 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
});
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.
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.
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.
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.
// inside a route handling function
req.event()
.on("end", function() {
// use req.body() here
});
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.
This function returns true if the client is currently connecting to the server. It is set to false when the client has succesfully connected.
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.
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.
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.
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.
// inside a route handling function and with "/home?data=example" as url
req.get("data");
// -> will return "example"
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".
With this function you can easily get a header. It is basically a shortcut for .headers().get(). You specify the header you want to get with the key parameter.
// inside a route handling function and with the header "Host: example.com"
req.header("host");
// -> will return "example.com"
This function will return the request.headers class. With this class you can get the headers that were sent in 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.
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.
// inside a route handling function and with "application/json" as content-type
req.is("html", "json");
// -> will return "json"
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.
This function returns the method that was used for the request.
// inside a route handling function and with "GET" as method
req.method()
// -> will return "GET"
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.
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.
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.
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.
This function returns if there is data readable from the request, meaning if there is data available to read.
This function will return the request.remote class for this request. This class contains information about the address of the user.
With this function you can check if the connection is secure, meaning it is over "https".
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.
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.
// inside a route handling function and with "/home" as url
req.url()
// -> will return "/home"
This function will return the HTTP version sent by the client. This is often the same as the version the server is running on.
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".