This is the server class. With you can easily create and manage a http server. This class is also needed when working with the cerus-router. When there is a request the callback function is called. This callback has to be set if you want to receive incoming requets. It must be noted that this name can be confusing and therefor a different module is on it's way. This means this class will be deprecated somewhere in the near future.
// in this example a server is created and started
cerus.server().callback(function(req, res) {});
// -> now all incoming request will call this function
cerus.server().start();
// -> starts the server on port 80
This function will return the address class for this server. The address class contains information about the address the server is using. This class can only be returned of the server is listening.
This is a getter and setter for the number of milliseconds of inactivity the server needs to wait for incoming data. If it takes more than the alive time the request is presumed inactive. The default time is 5000 milliseconds (5 seconds).
This function sets the callback function for the server. This function will be called when there is a new request. The function will be called with to parameters: req and res. The req parameter contains the request (http.IncomingMessage) and the res parameter the response (http.ServerResponse). For a custom and better req and res take a look at the cerus-router module.
cerus.server().callback(function(req, res) {});
// -> this will set the callback function
This function will return the current amount of connections. The connections will be returned using a promise. The amount of connections will be a parameter in the connections event. The amount of connections can only be fetched if the server is listening.
With this function you can listen for server events. It'll return a promise that is called on a number of events. The "error" event is called when there was an error in the server. It is called with the error as parameter. The "close" event is called when the server was closed. The "listening" event is called when the server has started listening. The "connection" event is called when there was a new connection together with the new socket as paramater.
This function returns if the server is currently listening for requests. This means that this function returns if the server has started or if it's currently stopped.
This is a getter and setter for the maximum amount of connections that the server will accept before refusing requests. By default there is no maximum amount of connections.
This is a getter and setter for the maximum amount of headers that the server will accept in a HTTP request. The default maximum amount of headers is 2000.
This is a getter and setter for the port that will be used for the server. It is possible to not use this port by supplying a port to the .start() function directly.
This function is used to start the server. You can also supply a port if you don't want to use the one that is currently set. The server cannot be started when it is already running. This function will also return a promise. This promise will call the "started" event when the server has been started.
cerus.server().start(); // or cerus.server().listen();
// -> starts the server on the default port 80
This function will stop the server. It can only be called if the server is already running. It'll also return a promise that calls the "stopped" event when the function has finished stopping the server.
// with a server already running
cerus.server().stop(); // or cerus.server().end();
// -> stops the server
This is a getter and setter for the number of milliseconds of inactivity a socket can use before being presumed to have timed out. When a socket is presumed to have timed out the connection is destroyed. The default time 120000 (2 minutes).