MasterController

Installation & Boot

Install the framework and understand the three-step boot sequence.

MasterController is pure ESM and targets Node.js 20+. In a Master app it’s already in the backend/ workspace; to add it to any project:

terminal
$ npm install mastercontroller
ESM only
Set "type": "module" in your package.json.

The boot sequence#

Every app starts the framework in three steps, in this order:

  1. setupServer() — creates the HTTP(S) server and wires the request pipeline.
  2. startMVC('app') — loads app/routes.js and pre-registers every controller in app/controllers/.
  3. start() — finalizes the pipeline and registers terminal routing.
server.js
import master from 'mastercontroller';
import { existsSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';

master.root = import.meta.dirname;
master.environmentType = process.env.NODE_ENV || 'development';

const server = master.setupServer('http');

// CORS for the Next.js frontend
master.cors.init({ origin: [process.env.FRONTEND_URL || 'http://localhost:3000'] });

// Auto-load middleware (alphabetical; numeric prefixes order them)
const mw = join(import.meta.dirname, 'middleware');
if (existsSync(mw)) {
  for (const f of readdirSync(mw).filter((x) => x.endsWith('.js')).sort()) {
    const mod = await import(pathToFileURL(join(mw, f)).href);
    if (typeof mod.default === 'function') master.pipeline.use(mod.default);
    else if (mod.default?.register) mod.default.register(master);
  }
}

await master.startMVC('app');
await master.start(server);

server.listen(parseInt(process.env.PORT, 10) || 3001);

HTTPS in production#

https
import { readFileSync } from 'node:fs';

const server = master.setupServer('https', {
  key: readFileSync('/path/privkey.pem'),
  cert: readFileSync('/path/fullchain.pem'),
});
master.enableHSTS({ maxAge: 31536000, includeSubDomains: true, preload: true });
Middleware loading
startMVC() loads routes + controllers, but not a middleware/ folder — register middleware yourself between setupServer() and start(), as shown above. The Master scaffold includes this loader by default.