|
| 1 | +# Express Runtime Interaction |
| 2 | + |
| 3 | +This page documents every supported way to interact directly with Arkstack's Express runtime. |
| 4 | + |
| 5 | +## Bootstrap Exports |
| 6 | + |
| 7 | +From `src/core/bootstrap.ts`: |
| 8 | + |
| 9 | +- `expressApp`: raw Express instance |
| 10 | +- `app`: Arkstack `Application` instance |
| 11 | + |
| 12 | +```ts |
| 13 | +import { app, expressApp } from 'src/core/bootstrap'; |
| 14 | +``` |
| 15 | + |
| 16 | +## Interaction Options |
| 17 | + |
| 18 | +### 1. Raw runtime: `expressApp` |
| 19 | + |
| 20 | +Use this for direct Express APIs. |
| 21 | + |
| 22 | +```ts |
| 23 | +import { expressApp } from 'src/core/bootstrap'; |
| 24 | + |
| 25 | +expressApp.set('trust proxy', 1); |
| 26 | +expressApp.disable('x-powered-by'); |
| 27 | +expressApp.locals.serviceName = 'api'; |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Instance runtime accessor: `app.getAppInstance()` |
| 31 | + |
| 32 | +Get the runtime instance from the Arkstack wrapper. |
| 33 | + |
| 34 | +```ts |
| 35 | +import { app } from 'src/core/bootstrap'; |
| 36 | + |
| 37 | +const runtime = app.getAppInstance(); |
| 38 | +runtime.use((req, _res, next) => { |
| 39 | + req.headers['x-runtime'] = 'express'; |
| 40 | + next(); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### 3. Static runtime accessor: `Application.getAppInstance()` |
| 45 | + |
| 46 | +Access the static runtime reference. |
| 47 | + |
| 48 | +```ts |
| 49 | +import Application from 'src/core/app'; |
| 50 | + |
| 51 | +const runtime = Application.getAppInstance(); |
| 52 | +runtime.set('etag', false); |
| 53 | +``` |
| 54 | + |
| 55 | +### 4. Driver interaction: `app.getDriver()` |
| 56 | + |
| 57 | +Use the runtime driver for lifecycle-level integration. |
| 58 | + |
| 59 | +```ts |
| 60 | +import { app } from 'src/core/bootstrap'; |
| 61 | + |
| 62 | +const driver = app.getDriver(); |
| 63 | +const runtime = app.getAppInstance(); |
| 64 | +await driver.applyMiddleware(runtime, (req, _res, next) => next()); |
| 65 | +``` |
| 66 | + |
| 67 | +### 5. Router contract interaction: `app.getRouter()` |
| 68 | + |
| 69 | +Use the framework-agnostic router contract. |
| 70 | + |
| 71 | +```ts |
| 72 | +import { app } from 'src/core/bootstrap'; |
| 73 | + |
| 74 | +const router = app.getRouter(); |
| 75 | +await router.bind(app.getAppInstance()); |
| 76 | + |
| 77 | +const routes = await router.list({ path: '/api' }); |
| 78 | +console.log(routes); |
| 79 | +``` |
| 80 | + |
| 81 | +### 6. Lifecycle control: `boot` and `shutdown` |
| 82 | + |
| 83 | +```ts |
| 84 | +import { app } from 'src/core/bootstrap'; |
| 85 | + |
| 86 | +await app.boot(3000); |
| 87 | +// later |
| 88 | +await app.shutdown(); |
| 89 | +``` |
| 90 | + |
| 91 | +## Notes |
| 92 | + |
| 93 | +- `app.boot(port)` mounts public assets, binds router, applies middleware, registers error handling, starts the server, and attaches graceful shutdown. |
| 94 | +- Use the router contract (`getRouter`) for framework-agnostic behavior where possible. |
| 95 | +- Prefer `expressApp` only when you specifically need native Express APIs. |
0 commit comments