allow null terminating handler

This commit is contained in:
Erik 2022-11-09 19:42:56 +02:00
parent 391775b702
commit 664127cdbc
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 5 additions and 2 deletions

View File

@ -112,7 +112,7 @@ class Server extends EventEmitter {
logRequest (req, res, next) {
res.once('finish', () => {
this.logger[res.statusCode === 401 ? 'unauthorised' : 'access'](`[${req.get('X-Forwarded-For') || req.socket.remoteAddress}] [STATUS: ${res.statusCode}] Request to ${req.route?.path || req.path}`);
this.logger[res.statusCode === 401 ? 'unauthorised' : 'access'](`[${req.get('X-Forwarded-For') || req.socket.remoteAddress}] [STATUS: ${res.statusCode}] [${req.user?.username || 'UNAUTHENTICATED'}] Request to ${req.route?.path || req.path}`);
});
next();
}

View File

@ -37,7 +37,10 @@ class Endpoint {
this.subpaths = [ ...this._subpaths, ...this.subpaths ];
for (const [ sub, method, cb, mw = [] ] of this.subpaths) {
if (typeof method !== 'string') throw new Error(`Invalid method parameter type in Endpoint ${this.name} subpath ${sub}`);
this.server.app[method](this.path + sub, ...this.middleware, ...mw, cb);
if (!this.middleware.length && !mw.length && !cb) throw new Error(`Cannot have endpoint with no handler and no middleware, expecting at least one to be defined`);
const args = [ this.path + sub, ...this.middleware, ...mw ];
if (cb) args.push(cb); // Sometimes (such as login endpoints) we don't necessarily want an explicit terminating handler, rather the middleware takes care of it
this.server.app[method](...args);
}
}