@horwood/server
This is a simple Node.js module that allows the user to quickly bootstrap a server utilising the Express framework.
Getting Started
It is available through the NPM registry by using the following command:
$ npm install @horwood/server
Running via the CLI
In order to run the server from the CLI, run:
$ horwood-server
The server must be provided with a server.conf module in order to function. By default, it looks in the root directory
of your project for a module named server.conf
. If you wish to override this, you can use the following command:
$ horwood-server --server-conf=./path/to/server.conf
Running Programmatically
To run the server programmatically, you must call Server.start using a valid ServerConf
.
const horwoodServer = require('@horwood/server');
const serverConf = new horwoodServer.ServerConf(require('./server.conf'));
horwoodServer.Server.start(serverConf);
Configuration
Configuration should be provided using the following format:
const port = 8080;
const serverConf = {
'logConf': {
'location': './logs',
'preserve': false,
'level': 'INFO'
},
'endpoints': [
{
'method': 'GET',
'path': '/',
'callback': (req, res) => {
res.json(serverConf.endpoints)
}
}
],
'port': port,
'prefix': '/api',
'staticConf': {
'prefix': '',
'root': 'dist'
},
'proxies': [
{
'path': 'endpoints',
'destination': `http://localhost:${port}/api`
}
],
'sockets': true,
'socketsCallback': (io) => {
io.on('connection', (socket) => {
socket.emit('testEvent');
});
}
};
module.exports = serverConf;
Valid configuration properties are listed below.
Property | Description | Options | Default |
---|---|---|---|
logConf.location | Where log files will be stored. | ./logs | |
logConf.preserve | Whether or not to preserve old logs. | true false |
false |
logConf.level | The level of logs to output. | VERBOSE INFO WARN ERROR |
INFO |
endpoints | A list of endpoints to be mapped. | [] | |
endpoint.method | The HTTP method to use for the endpoint. | DELETE GET POST PUT |
|
endpoint.path | The path to use for the endpoint. | ||
endpoint.callback | The function to call when the endpoint is hit. | ||
port | The port to deploy the server on. | 8080 | |
prefix | The prefix to prepend all endpoints with. | /api | |
staticConf.prefix | The prefix to prepend to requests for static content. | ||
staticConf.root | The root of the static content. | ||
proxies | A list of proxies to be configured. | [] | |
proxy.path | The path to use for the proxy. | ||
proxy.destination | Where to send the request to when the path is hit. | ||
sockets | A flag stating whether sockets should be enabled. | true false |
false |
socketsCallback | A callback that is called when Socket IO is ready. |