v2.1.0
Hooks added
Now it is possible to register hook functions for some events, like:
'before-setup'
: It'll be emitted when the internalExpressjs
instance is created, but before it gets configured.'before-api-setup'
: It'll be emitted right before theAPI
resources are set.'after-api-setup'
: It'll be emitted when all theAPI
resources have been set.'before-static-setup'
: It'll be emitted right before thestatic
resources are set.'after-static-setup'
: It'll be emitted when all thestatic
resources have been set.'after-setup'
: It'll be emitted when all the configurations were applied and the server is about to get initialized.
The hook functions for these two events will receive the internal express instance
, and the express module
as arguments.
The Configurator.prototype.on(event, listener)
should be used in order to register listeners for those events.
Example
const Configurator = require('w-srvr');
const server = new Configurator();
server
.port(8080)
.on('before-setup', function(app, express){
// *Do something cool here...
})
.start()
.then(({ address }) => console.log('Server started at ' + address))
.catch(err => console.error(err));
Note: For convenience, the Configurator.HOOKS
enum has all the available hook events names.
404 handling
With the Configurator.prototype.notFound(middleware)
it is now possible to register middlewares to handle 404 NOT FOUND
status
Example
server
.notFound(function(req, res, next){
// *Do something cool here...
});
Index page behaviour changes
Now, when setting the index page of your app, you can choose if this page should be served only at the root route (i.e. http://localhost/), or at all the available routes (i.e. those that normally would return a 404
response). It is useful if you are developing a single page application
with routing control built at the client.
Example
You can add this feature with the root_only
option:
server.static
.index('../src/index.html', { root_only: false }); // Will serve at all the available routes
Note: The initial value is true
(i.e. it serves only at the root route).