Path and Params | URL Relative Endpoint (URL path) | Functional Endpoint (method/function) |
---|---|---|
/convert/temperature?unit=c&value=10 | /convert/temperature | convert |
/convert/length?unit=m&value=2 | /convert/length | convert |
/translate/english?word=hello | /translate/english | translateEnglish |
http://localhost:3000/convert/length?unit=m&value=2
route() { const path = this.url.path(); if (path === '/convert/temperature') this.convert(TemperatureConverter); else if (path === '/convert/length') this.convert(LengthConverter); else if (path === '/translate/english') this.translateEnglish(); else log(`URL Path not handled: ${path}`); }
convert(converterClass) { const query = this.url.query(), unit = query.unit, value = query.value; if (!this.validateParam('unit', unit) || !this.validateParam('value', value)) return; const converter = new converterClass(unit, value); this.serveConversionResults(converter.getResults()); }
serveConversionResults(arrayResults) { //serve result to client/consumer const res = this.response; res.writeHead(statusCodes.OK, htmlContentType); //result is an array for (let pair of arrayResults) res.write(`${pair.value} ${pair.unit}` + br); res.end(); }
2 m 6.56168 ft 2000 mm 200 cm 0.002 km 78.74016 in 0.00124 mi 2.18723 yd