The Configurator
class is the main resource of this project and serves as the entry point to setup the Express server.
const server = new Configurator();
object - A static enum of strings that has all the supported HTTP methods names, which are GET
, POST
, PUT
, DELETE
, HEAD
, PATCH
and OPTIONS
You can use it with the API configurator to add a route middleware:
server.api
.add(Configurator.METHODS.GET, '/some-route', someMiddlewareFunction);
This will register a middleware for a GET
request on '/some-route'
.
object - A static enum of all the available hooks events names, which are (in the order they're executed):
BEFORE_SETUP
- It'll be emitted when the internalExpressjs
instance is created, but before it gets configuredBEFORE_API_SETUP
- It'll be emitted right before theAPI
resources are setAFTER_API_SETUP
- It'll be emitted when all theAPI
resources have been setBEFORE_STATIC_SETUP
- It'll be emitted right before thestatic
resources are setAFTER_STATIC_SETUP
- It'll be emitted when all thestatic
resources have been setAFTER_SETUP
- It'll be emitted when all the configurations were applied and the server is about to get initialized
Note: All the hook functions will receive the express instance
, and the express module
as arguments.
Note: You can also use the hook event name itself, they are: 'before-setup'
, 'before-api-setup'
, 'after-api-setup'
, 'before-static-setup'
, 'after-static-setup'
and 'after-setup'
.
Using with the Configurator.prototype.on(event, listener)
to register hook functions:
server
.on(Configurator.HOOKS.BEFORE_API_SETUP, (app, express) => {
// *Do something cool...
})
.on('before-api-setup', (app, express) => {
// *Do something cool here...
});
number|string - The server port
Note: readonly, use the port()
method to assign a new value.
StaticConfigurator - The inner configurator for static resources
APIConfigurator - The inner configurator for API routes
Sets the port number of the server.
Note: by default, the initial server port is 80
.
port_number
number|string - The port number
Configurator - This same configurator (for method chaining)
console.log(server.server_port); // Will print 80 (default port)
server.port(3000); // Will set the port to 3000
server.port('8080'); // Will set the port to '8080'
server.port(process.env.PORT); // Will use the PORT environment variable
Registers middlewares to handle 404 responses.
Note: it can be used to send custom 404 pages or API responses.
Note: you can call this method multiple times to add more middlewares, but you can also simply pass an array of middlewares.
middleware
function|function[] - A valid Expressjs middleware function
Configurator - This same configurator (for method chaining)
server
.notFound((req, res, next) => {
// *Do something cool here...
});
Switches the server to use HTTPS.
Note: calling this method will make the default server port to be 443
instead of 80
.
options
object - The HTTPS options objectoptions.cert
string - The HTTPS certificateoptions.key
string - The HTTPS keyoptions.pfx
string - The HTTPS pfxoptions.passphrase
string - The HTTPS pfx passwordis_file
boolean - Whether the options values are filenames, and should be retrieved from disk
Configurator - This same configurator (for method chaining)
Using key and certificate files:
server
.https({
key: './my-key.key',
cert: './my-cert.crt'
}, true)
// ...
Using a PFX file:
server
.https({
pfx: './my-pfx.pfx',
passphrase: './my-pass.txt'
}, true)
// ...
Sets a new property in the Express locals
(or the entire locals
object).
Note: if an object is passed, it will override the locals
object. Otherwise, the arguments will be processed as a key and a value, and they will be merged into the locals
object.
key
object|string - The key of the value, or the entirelocals
objectvalue
* - The value for the given key
Configurator - This same configurator (for method chaining)
Setting the entire locals
object:
server
.locals({
prop1: 'val1',
prop2: 'val2'
})
// other calls to 'locals()' method passing an object will override it
Using key-value pairs:
server
.locals('prop1', 'val1')
.locals('prop2', 'val2')
// calls to 'locals()' method passing an object will override it
Registers a handler for a given event.
Note: it can be used to configure hook functions.
See: Configurator.HOOKS
for all the hooks events available.
event
string - The event namelistener
function - The handler function
Configurator - This same configurator (for method chaining)
server
.on('before-api-setup', (app, express) => {
// *Do something cool here...
});
Starts the server instance.
Note: It will apply the settings and start the server, so it should be called after the configurations have been done.
Note: These are the actions done in order to start the web server:
-
For each API resource:
-
Set parsers
-
Set preflight headers (in an
OPTIONS
middleware) -
Set other headers
-
Set the middlewares
-
For each static resource:
-
Set a static
GET
middleware -
Set the index page on the root route (
/
) -
Set an auto
404
response middleware
Promise<{ server, address }> - The promise resolves into an { server, address }
object, or it rejects with a Error
if the server could not be started:
server
http.Server - The started HTTP serveraddress
URL - The server address information
server
.port(3000)
.start() // It will commit all settings and start the server
.then(output => console.log('Server started at ' + output.address.href))
.catch(err => console.error(err));
Stops the current server instance.
Note: It will end all connection sockets remaining in the current server, and release the listened port.
Promise - The promise resolves if everything goes ok, or it rejects with a Error
if the server could not be stopped