# Class: NodeApiServer

## NodeApiServer

#### new NodeApiServer()

The NodeApiServer class.

Source:
  - NodeApiServer.js, line 34

### Methods

- (static) access(dbName, objType, objName, methodName, checkFunction) → {*}
- express() → {*|exports|module.exports}
- handleRequest(req, res)
- handleSyncRequest(req, res)
- hasPermission(dbName, objType, objName, methodName, req, callback) → {*}
- init(core)
- sendToAll(io, eventName, data)
- sendToClient(res, messageId, eventName, stringifiedData)
- serverApp() → {*}
- start(host, port, options, callbackopt) → {NodeApiServer}
- stop()

#### (static) access(dbName, objType, objName, methodName, checkFunction) → {*}

Defines an access rule for an object and HTTP method combination. When access is requested via a REST call, the function provided will be executed and the callback from that method will determine if the access will be allowed or denied. Multiple access functions can be provided for a single model and method allowing authentication checks to be stacked. This call also allows you to pass a wildcard "*" instead of the objType, objName and methodName parameters which will match all items. This allows you to set permissions globally. If no access permissions are present ForerunnerDB will automatically deny any request to the resource by default.

##### Parameters:

Name | Type | Description
--- | --- | ---
`dbName` | String | The name of the database to set access rules for.
`objType` | String | The type of object that the name refers to e.g. "collection", "view" etc. This effectively maps to a method name on the Db class instance. If you can do db.collection() then you can use the string "collection" since it maps to the db.collection() method. This means you can also use this to map to custom classes as long as you register an accessor method on the Db class.
`objName` | String | The object name to apply the access rule to.
`methodName` | String | The name of the HTTP method to apply the access function to e.g. "GET", "POST", "PUT", "PATCH" etc.

`checkFunction`

function | String

The function to call when an access attempt is made against the collection. A callback method is passed to this function which should be called after the function has finished processing. If you do not need custom logic to allow or deny access you can simply pass the string "allow" or "deny" instead of a function and ForerunnerDB will handle the logic automatically.

Source:
  - NodeApiServer.js, line 742

##### Returns:

Type
  *

#### express() → {*|exports|module.exports}

Gets the express library.

Source:
  - NodeApiServer.js, line 194

##### Returns:

Type
  * | exports | module.exports

#### handleRequest(req, res)

Handles requests from clients to the endpoints the database exposes.

##### Parameters:

Name | Type | Description
--- | --- | ---
`req` |  |
`res` |  |

Source:
  - NodeApiServer.js, line 316

#### handleSyncRequest(req, res)

Handles client requests to open an EventSource connection to our server-sent events server.

##### Parameters:

Name | Type | Description
--- | --- | ---
`req` |  |
`res` |  |

Source:
  - NodeApiServer.js, line 595

#### hasPermission(dbName, objType, objName, methodName, req, callback) → {*}

Checks for permission to access the specified object.

##### Parameters:

Name | Type | Description
--- | --- | ---
`dbName` | String |
`objType` | String |
`objName` | String |
`methodName` | String |
`req` | Object |
`callback` | function |

Source:
  - NodeApiServer.js, line 722

##### Returns:

Type
  *

#### init(core)

The init method that can be overridden or extended.

##### Parameters:

Name | Type | Description
--- | --- | ---
`core` | Core | The ForerunnerDB core instance.

Source:
  - NodeApiServer.js, line 42

#### sendToAll(io, eventName, data)

Sends server-sent-events message to all connected clients that are listening to the changes in the IO that is passed.

##### Parameters:

Name | Type | Description
--- | --- | ---
`io` |  |
`eventName` |  |
`data` |  |

Source:
  - NodeApiServer.js, line 677

#### sendToClient(res, messageId, eventName, stringifiedData)

Sends data to individual client.

##### Parameters:

Name | Type | Description
--- | --- | ---
`res` |  |
`messageId` |  |
`eventName` |  |
`stringifiedData` |  | Data to send in already-stringified format.

Source:
  - NodeApiServer.js, line 704

#### serverApp() → {*}

Gets the express app (router).

Source:
  - NodeApiServer.js, line 186

##### Returns:

Type
  *

#### start(host, port, options, callbackopt) → {NodeApiServer}

Starts the rest server listening for requests against the ip and port number specified.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`host` | String |  | The IP address to listen on, set to 0.0.0.0 to listen on all interfaces.
`port` | String |  | The port to listen on.
`options` | Object |  | An options object.
`callback` | function | <optional> | The method to call when the server has started (or failed to start).

Source:
  - NodeApiServer.js, line 89

##### Returns:

Type
  NodeApiServer

#### stop()

Stops the server listener.

Source:
  - NodeApiServer.js, line 299