serve(results) { //serve in JSON format const res = this.response; res.writeHead(statusCodes.OK, jsonType); res.end(JSON.stringify(results)); }
[{"unit":"m","value":2}, {"unit":"ft","value":6.56168}, {"unit":"mm","value":2000}, {"unit":"cm","value":200}, ...]
authenticate() { //check API key //simplified authentication to exemplify const expected = 'Z283794', res = this.response; if (this.url.query().apiKey !== expected) { res.writeHead(statusCodes.BAD_REQUEST, plainType); res.end("Invalid API key\n"); return false; } return true; }
processRequest() { if (!this.authenticate()) return; this.logRequestInfo(); this.route(); }