In Node..js module, export
is a global object containing all “public” variables, objects and functions you want to pass from the module.
You can also use the global
object, if necessary. In QetriX I use it for mocking location
object, available on frontend.
I finally created a ready-to-go backend API, that use a simplest 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.
Loop through pre-compiled regexes or patterns and pass matches using the request context
Or use directly the HTTP method:
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.
Split the path on /
and then switch on the contents of the path segments
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.