Friday, August 28, 2020

Export in Node.js

In Node..js module, export is a global object containing all “public” variables, objects and functions you want to pass from the module.

  1. export.func = func.constructor;

You can also use the global object, if necessary. In QetriX I use it for mocking location object, available on frontend.

Friday, August 21, 2020

Universal API

I finally created a ready-to-go backend API, that use a simplest routing.

Saturday, August 15, 2020

HTTP routing

http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html

https://github.com/nikic/FastRoute

https://benhoyt.com/writings/go-routing/

You have several options, how to route HTTP requests. My habit is to find the fastest way, but it's not needed here, because the router is called only once on URL change.

Table

Loop through pre-compiled regexes or patterns and pass matches using the request context

  1. router.addRoute(httpMethod, "a/(*.)/c", routeHandler),

Or use directly the HTTP method:

  1. router.get("a/(*.)/c", routeHandler);

Switch

A switch statement with cases that call a regex-based match() helper which scans path parameters into variables.

Similar to regex match, but using a simple pattern matching function instead of regexes. It's not that powerful, but you will mostly need “anything” wildcard for a path part anyway.

  1. case httpMethod == "GET" && routeRegexPattern.match("a/(*.)/c"):
  2. return routeHandler();

Split

Split the path on / and then switch on the contents of the path segments

  1. case httpMethod == "GET" && p[0] == "a" && p[1] == "b":
  2. return routeHandler();

Shift

Basically delegates the route matching to the module, according to the first path part. For a/b/c path the router will call an “a” module, pass him [b,c] and don't care any more.