Skip to content

Web Handlers

Web handlers provide a way to easily add additional routes to your Web Server.

Inherit from WebHandlerModule, export moduleInfo and getModule, then register your route(s) inside init():

mods/web_handlers/my_handler.js
const WebHandlerModule = require('../../core/web_handler_module');
exports.moduleInfo = {
name: 'My Handler',
desc: 'Does something custom',
author: 'You',
packageName: 'com.example.my-handler',
};
exports.getModule = class MyWebHandler extends WebHandlerModule {
constructor() {
super();
}
init(webServer, cb) {
super.init(webServer, err => {
if (err) { return cb(err); }
this.webServer.addRoute({
method: 'GET',
path: /^\/my-path\/?$/,
handler: this._handleRequest.bind(this),
});
return cb(null);
});
}
_handleRequest(req, resp) {
const body = JSON.stringify({ hello: 'world' });
resp.writeHead(200, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
});
resp.end(body);
}
};

Add an entry under contentServers.web.handlers in config.hjson. The key is the camelCase form of your moduleInfo.name:

contentServers: {
web: {
handlers: {
myHandler: {
enabled: true
}
}
}
}

Restart ENiGMA and your route will be registered alongside the built-in handlers.