Skip to content

[Bug] Split API app construction from server startup #1771

Description

@LukastGeorge

References #33 and #221.

/bounty $50

Problem

apps/api/src/index.ts builds the Express app and immediately calls app.listen(...) at module top level. That is fine for local npm run dev, but it makes the API entrypoint hard to import from tests or scripts because importing the module has a side effect: it binds a real port.

Current behavior couples app construction with process startup:

const app = express();
const port = process.env.PORT || 4000;

app.use(express.json());
// routes...

app.listen(port, () => {
  console.log(`TaskFlow API listening on port ${port}`);
});

Why it matters

A future route test suite should be able to import the Express app and call routes in memory. With the current structure, importing the module can cause:

  • port conflicts when more than one test imports the module;
  • hanging tests because the listener stays open;
  • harder route-level test setup because app creation and process startup are coupled.

Expected behavior

The Express app should be exportable without starting a listener. Server startup should only happen in the dev/runtime entrypoint.

Suggested fix

Split app construction from server startup:

  • apps/api/src/app.ts: creates/configures app, exports it.
  • apps/api/src/index.ts: imports app and calls listen for runtime/dev usage only.

Acceptance criteria

  • Importing the Express app does not bind a port.
  • npm run dev -w apps/api still starts the API server normally.
  • Existing /health and /users behavior stays unchanged.
  • Add a small test or smoke script demonstrating the app can be imported without starting the listener.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions