Server

Server

Creates a new HTTP & HTTPS Server that supports multiple ports.

Constructor

new Server(optionsopt, connectionListeneropt) → {Server}

Source:
Example

Create a server on port 8080 that is automatically listening

server = new Server({ port: 8080, listen: true })
Parameters:
Name Type Attributes Description
options Object <optional>

The options for the server or connectionListener.

Properties
Name Type Attributes Description
useAvailablePort Boolean <optional>

Whether to use the next available port if the desired port is unavailable.

listen Boolean <optional>

Whether to listen automatically.

listen Number <optional>

Whether to listen automatically, and to define the desired port or ports to use.

port Array | Number <optional>

The desired port or ports to use.

cert Buffer | String <optional>

The certificate; when missing along with options.key will cause a new certificate to be generated.

key Buffer | String <optional>

The key; when missing along with options.cert will cause a new certificate to be generated.

connectionListener function <optional>

The listener bound to all connections.

Returns:
Type
Server

Extends

  • https.Server

Members

listening :Boolean

Source:

Returns whether the server is currently listening.

Type:
  • Boolean
Example

Check if the server is currently listening.

server.listening // true or false

port :Array

Source:

Returns or assigns the ports currently being requested.

Type:
  • Array
Examples

Get the current ports in use.

server.port // [80, 443]

Set the current ports in use.

server.port = 80
server.port // [80]

Methods

close(closeListeneropt) → {Server}

Source:

Stops the server from accepting new connections.

Example
server.close(() => {
  console.log(`httpe has closed…`)
})
Parameters:
Name Type Attributes Description
closeListener function <optional>

The method called when all of the servers have been unbound.

Returns:
Type
Server

listen(optionsopt, listeneropt) → {Server}

Source:

Starts a server listening for connections.

Example
server.listen(() => {
  console.log(
    `httpe is listening…\n` +
    `--------------------------------------\n` +
    `   Local: ${server.port.map(port => `http://localhost:${port}/`).join('\n          ')}\n` +
    `External: ${server.port.map(port => `http://${httpe.ip}:${port}/`).join('\n          ')}\n` +
    `--------------------------------------`
  )
})
Parameters:
Name Type Attributes Description
options function | Options | Port <optional>

The port used for connections, the options for the server, or the listener called once the server is bound.

listener function <optional>

The listener called once the server has been bound.

Returns:
Type
Server

use(patternopt, callbackopt) → {Server}

Source:

Attach visitor functions on requests to the server.

Examples
server.use((req, res) => {
  // do something with any request
})

Do

server.use('/\**.js', (req, res) => {
  // do something with any request ending in .js
})
Parameters:
Name Type Attributes Description
pattern Object | String | RegExp <optional>

The pattern used to match requests.

callback function <optional>

The method called whenever a request is made and/or matched.

Returns:
Type
Server