.server()
Access:
public

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.

Examples

// 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

Summary

Name
Description
Returns the server.address class.
The getter/setter for the keep alive timeout time.
Sets the callback that will be called for incoming requests.
Returns the amount of active connections.
Returns a promise that is called on every event.
Returns if the server is listening.
The getter/setter for the maximum amount of connections.
The getter/setter for the maximum amount of headers.
The getter/setter for the server port.
Starts the server.
Stops the server.
The getter/setter for the timeout time.

Functions

.address()
Access:
public
Summary:
Returns the server.address class.

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.

Returns

Type
Class
Description
The server.address class.
.alive(alive)
Access:
public
Summary:
The getter/setter for the keep alive timeout time.

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).

Parameters

Name
Type
Description
alive
Number
The keep alive timeout time. (optional)

Returns

Type
Number
Description
The keep alive timeout time.
.callback(func)
Access:
public
Summary:
Sets the callback that will be called for incoming requests.

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.

Parameters

Name
Type
Description
func
Function
The callback function.

Examples

cerus.server().callback(function(req, res) {});
// -> this will set the callback function
.connections()
Access:
public
Summary:
Returns the amount of active connections.

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.

Returns

Type
Number
Description
The amount of connections.
.event()
Access:
public
Summary:
Returns a promise that is called on every event.

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.

Returns

Type
Promise
Description
This function will return a promise.
.listening()
Access:
public
Summary:
Returns if the server is listening.

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.

Returns

Type
Boolean
Description
If the server is listening.
.maxconnections(maxconnections)
Access:
public
Summary:
The getter/setter for the maximum amount of connections.

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.

Parameters

Name
Type
Description
maxconnections
Number
The new maximum amount of connections. (optional)

Returns

Type
Number
Description
The maximum amount of connections.
.maxheaders(maxheaders)
Access:
public
Summary:
The getter/setter for the maximum amount of headers.

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.

Parameters

Name
Type
Description
maxheaders
Number
The new maximum amount of headers. (optional)

Returns

Type
Number
Description
The maximum amount of headers.
.port(port)
Access:
public
Summary:
The getter/setter for the server port.

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.

Parameters

Name
Type
Description
port
Number
The new server port. (optional)

Returns

Type
Number
Description
The server port.
.start(port)
Aliases:
listen
Access:
public
Summary:
Starts the server.

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.

Parameters

Name
Type
Description
port
Number
The port that the server will use. (optional)

Examples

cerus.server().start(); // or cerus.server().listen();
// -> starts the server on the default port 80

Returns

Type
Promise
Description
This function will return a promise.
.stop()
Aliases:
end
Access:
public
Summary:
Stops the server.

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.

Examples

// with a server already running
cerus.server().stop(); // or cerus.server().end();
// -> stops the server

Returns

Type
Promise
Description
This function will return a promise.
.timeout(timeout)
Access:
public
Summary:
The getter/setter for the timeout time.

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).

Parameters

Name
Type
Description
timeout
Number
The timeout time. (optional)

Returns

Type
Number
Description
The timeout time.