APIs with Express · Chapter 21 of 29

App settings

By the end of this chapter you can configure an Express app from the environment, make its routes match exactly, and see the real client behind a load balancer without trusting forged headers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 tasks
  • Express
  • In the all labs plan

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.

Why you need this

Tapri's API moves from Kabir's laptop to real servers behind a load balancer, and three things break on the first morning. The port is fixed at 3000, but the platform hands out its own. Every request seems to come from 10.0.0.5, the load balancer, so the rate limiter from chapter 19 treats all cafés as one caller. And the app thinks every request is plain http, although customers use https.

With what you know, you would edit the code for each machine, and read X-Forwarded-For yourself, trusting whatever a client writes in it.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    loadConfig: the port, mode and café name, checked

    Export loadConfig(env), which reads an environment object (process.env in real life, any object in the check) and returns a frozen object { port, mode, cafeName, trustProxy } (trustProxy is task 5).

    port: Number(env.PORT), or 3000 when PORT is missing. It must be a whole number from 1 to 65535: 'abc', '0', '70000', '80.5' and '-1' throw an Error whose message contains PORT. '8080' gives the number 8080.

    mode: env.NODE_ENV, or 'development' when missing. It must be development, test or production: 'staging' throws an Error whose message contains NODE_ENV.

    cafeName: env.CAFE_NAME, or 'Tapri' when missing.

    Object.isFrozen on the result is true.

  2. Task 2

    createApp: the config in settings and app.locals

    createApp(config) receives a config like the one from task 1 (the check builds its own). Set the env setting to config.mode, the config setting to the very object you were given, and app.locals.cafeName to config.cafeName, and turn x-powered-by off, so no response has an X-Powered-By header.

    GET /about (already written) then answers { cafe: 'Tapri Indiranagar', mode: 'production' }. In production its JSON is compact, exactly '{"cafe":"Tapri Indiranagar","mode":"production"}'. In development only, set json spaces to 2, so it is indented by two spaces: '{\n "cafe": "Tapri",\n "mode": "development"\n}'.

  3. Task 3

    createApp: routes that match exactly

    Turn on the case sensitive routing and strict routing settings, so app.enabled reports true for both. GET /menu answers the menu, while /Menu, /MENU, /menu/ and /about/ answer 404.

    Express reads these two settings when it creates the app's router, at the first route or middleware, so they must be set before any app.get.

  4. Task 4

    createApp: the original protocol and host behind the load balancer

    Set the trust proxy setting to config.trustProxy, unchanged: with trustProxy 'loopback', app.get('trust proxy') is 'loopback'.

    GET /whoami (already written) answers { ip, protocol, secure, hostname }. With trustProxy 'loopback' and the headers X-Forwarded-Proto: https and X-Forwarded-Host: api.tapri.in, it answers protocol 'https', secure true and hostname 'api.tapri.in'. With trustProxy false, the same headers are ignored: protocol 'http', secure false, hostname '127.0.0.1'.

  5. Task 5 · interview question

    Interview: the real client IP, however the header is forged

    loadConfig reads env.TRUST_PROXY into trustProxy: missing, '' or 'false' gives false; only digits gives that number of hops as a Number ('1' gives 1, '2' gives 2); anything else stays the text it was ('10.0.0.0/8', 'loopback').

    With TRUST_PROXY '1' and X-Forwarded-For '6.6.6.6, 203.0.113.9', GET /whoami answers ip '203.0.113.9': the customer forged 6.6.6.6, and the load balancer added the address it saw. With '2' and '6.6.6.6, 198.51.100.4, 10.1.2.3' it answers '198.51.100.4'. With 'false', X-Forwarded-For is ignored and the ip is the local address (127.0.0.1 or ::1).

What you will learn

  • The settings table: app.set and app.get, the names Express itself reads, and app.locals.
  • Configuration from the environment: strings in, checked values out, and a clear failure at start-up.
  • Routing rules: case sensitive and strict routing, and the moment Express reads them.
  • trust proxy: the original IP, protocol and host behind a load balancer, and how many hops to trust.

More in Production

Build "App settings" yourself

Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.