Skip to content

XYZ API ​

Guide draft xyz api architecture express

The Express router, middleware chain and mod/ modules of the XYZ API, the request flow, and the endpoints MAPP and MAPP.UI call; use before changing a route, middleware or API module.

Resource and sources

XYZ is the Node.js API in apps/xyz (@geolytix/xyz-app). It serves the workspace, runs SQL query templates, authenticates users, and serves the MAPP views and bundles. MAPP and MAPP.UI are its browser clients; this page is the server side of that boundary.

Entry points ​

  • router.js createRouter(middleWare) returns an Express router; the middleware array runs before each endpoint module.[1]
  • server.js creates the router with the default chain rootRedirect, validateRequestParams, validateRequestAuth, mounts it, and listens on PORT unless it runs on Vercel. It exports the app as default and the router as xyzRouter.
  • A host app extends XYZ instead of forking it. apps/auth creates its own router with createRouter() and adds custom routes; apps/saml imports the default app and adds the ${DIR}/saml/* routes to it.
  • The package exports only ./router, ./server and ./mod/*. Importing mod/utils/processEnv.js assigns the frozen globalThis.xyzEnv. Modules read configuration from xyzEnv. Exceptions read process.env directly: processEnv.js itself, telemetry.js for the standard OTEL_* and platform variables, and _workspace.js for VITEST. See Server and the other environment concepts.

Request flow ​

Router-level handlers run in this order for every request:

  1. cors adds CORS headers for origins in CORS_ORIGINS; without it, it does nothing.[2]
  2. The rate limiter (RATE_LIMIT, RATE_LIMIT_WINDOW), then the cookie parser.
  3. ${DIR}/api responses get Cache-Control: private, no-cache.
  4. Static files: ${DIR}/public and ${DIR} from public/.
  5. requestSpan traces the rest; then body parser, middleware chain and endpoint.

The default chain:

  • rootRedirect redirects / to DIR.
  • validateRequestParams merges path and query params, rejects keys outside [A-Za-z0-9_-], deletes a user param, parses true/false/null and [a,b] arrays, defaults language to en, and stores the result on req._params.[3]
  • validateRequestAuth copies req._params to req.params and sets req.params.user from a token or cookie. An auth error returns 401. A PRIVATE instance redirects anonymous requests to AUTH_PATH or /api/user/login.[4]

The workspace, query, user, provider, sign and view modules also copy req._params, so they work behind a chain without validateRequestAuth.

Routes and modules ​

RouteModuleNotes
api/workspace{/:key}workspace/_workspace.jslocales, locale, layer; admin only: scopes, test, compose, templates. roles returns 410.
api/query{/:template}query.jsGET and POST. Runs a SQL template against a DBS_* connection.
api/user{/:method}{/:key}user/_user.jsAccount methods, eg. admin, cookie, key, token, update.
api/user/login, logout, register, verify{/:key}user/Mounted before api/user and without the middleware chain.
api/provider{/:provider}provider/_provider.jscloudfront, file, s3.
api/sign{/:signer}sign/_sign.jscloudfront, cloudinary, file, s3, plus one file signer per SIGN_* variable.
view{/:template}, ${DIR}/, /view.jsServes a view template; default_view without a template.

All routes are prefixed with DIR. Each dispatcher only calls a method which is an own member of its method map, and rejects any other key.

Modules behind the routes ​

  • workspace/ loads the workspace from WORKSPACE with cache.js, caches it for WORKSPACE_AGE, and resolves src references through provider/getSrc.js. composeObj.js merges templates and applies roles for the requesting user; getLocale, getLayer and getTemplate build on it. See Templates and workspace environment.
  • workspace/templates/ holds the built-in templates. _queries.js lists the SQL templates and flags (layer, admin, value_only). Layer formats use wkt, geojson, mvt and cluster; locations use location_get, location_new, location_update and location_delete; filters use distinct_values and field_minmax. _views.js maps view keys to public/views/*.html.
  • query.js composes the template for the user, resolves the layer for layer templates, builds the SQL filter with sqlFilter.js, substitutes ${param} as whitelisted strings and %{param} as $n values, and sends the query to dbs.js. See Layer data source and Databases.
  • user/ holds the ACL (acl.js), token and cookie authentication (auth.js) and the account methods. See JWT claims.
  • view.js substitutes in the view with whitelisted string params and dir, authPath, login and title from xyzEnv.

How XYZ serves MAPP ​

  • The default_view sets data-dir="" on the head and loads public/js/lib/mapp.js and ui.js; MAPP reads mapp.host from it.
  • The view script requests api/workspace/locales and api/workspace/locale?layers=true. MAPP requests api/workspace/layer for a location's layer and api/query for features and locations. MAPP.UI adds filter templates, api/user/* and api/sign/cloudinary.
  • The client only sends template and layer keys. SQL, dbs and role restrictions stay in the workspace on the server.
  • The bundles are build outputs; see Monorepo. For pages on another origin see the @geolytix/mapp package.

Where to change ​

  • A new endpoint: a route in router.js, a module under mod/, and a test in router.test.mjs.
  • A new built-in query: a template file and an entry in _queries.js. A deployment adds its own in workspace.templates instead.
  • Param or auth rules: the middleware modules. A host which builds its own chain must keep validateRequestParams before validateRequestAuth.
  • A new environment variable: processEnv.js and an environment concept.
  • Tests: apps/xyz/tests mirrors mod/; run pnpm test:xyz, see TESTING.md.

  1. apps/xyz/router.js ↩︎

  2. apps/xyz/mod/middleware/cors.js ↩︎

  3. apps/xyz/mod/middleware/validateRequestParams.js ↩︎

  4. apps/xyz/mod/middleware/validateRequestAuth.js ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.