TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/factus_challenge/llms.txt
Use this file to discover all available pages before exploring further.
bc-v1/ directory contains the Node.js backend for the Factus Challenge project. It is built on Express 4 and requires Node.js 22 or later. The server listens on port 4500 by default (overridable via the PORT environment variable), connects to a PostgreSQL database, and communicates with the Factus electronic invoicing API using OAuth 2.0 tokens managed by the src/auth/token.js module.
Prerequisites
Before running the backend, make sure you have the following installed and available:- Node.js ≥ 22 — the server uses the native
--env-fileand--watchflags introduced in Node 22 - npm — bundled with Node.js
- PostgreSQL — a running instance accessible from the machine where the backend will run
Installation
Navigate to the backend directory
All backend source code lives under
bc-v1/. Move into it before running any commands.Install dependencies
Install the five production dependencies with npm:The following packages will be installed:
| Package | Version | Purpose |
|---|---|---|
axios | ^1.7.9 | HTTP client for Factus API requests |
express | ^4.21.2 | Web framework and router |
morgan | ^1.10.0 | HTTP request logger middleware |
pg | ^8.13.1 | PostgreSQL client (pg.Pool) |
qs | ^6.14.0 | Query-string serialization for OAuth form data |
Create the .env file
The server loads environment variables from a
.env file in the bc-v1/ root using Node’s native --env-file flag. Create the file and populate every variable before starting:.env
refresh_token is optional at startup. token.js falls back to the password grant when it is absent and writes the received refresh_token directly into process.env for subsequent refreshes.Available npm Scripts
Thepackage.json exposes five scripts:
| Script | Command | Description |
|---|---|---|
npm start | node ./src/main.js | Production start — no hot reload, no env-file flag |
npm run dev | node --env-file .env --watch ./src/main.js | Development mode with hot reload via --watch |
npm run fact | node --env-file .env ./src/controllers/factura.controller.js | Run factura.controller.js as a standalone script |
npm run token | node --env-file .env --watch ./src/auth/token.js | Run the token module standalone with hot reload |
npm test | node --env-file .env --watch ./src/helpers/helpers.mjs | Run the helpers module standalone with hot reload |
Route Structure
The Express app mounts three routers, registered in order insidesrc/main.js:
Page Routes
GET /Served by page.routes.js → page.controller.js. Returns the frontend entry point.Invoice Routes
/factura*Served by factura.routes.js. Handles all Factus API invoice operations (list, get, create, download, delete).Database Query Routes
/get-data, /add-data, /get-join, /update-data, /deleteServed by querys.routes.js. Generic CRUD endpoints backed by pg.Pool.Full Route Map
| Method | Path | Router file | Description |
|---|---|---|---|
GET | / | page.routes.js | Serve frontend |
GET | /factura | factura.routes.js | List recent invoices |
GET | /factura/:number | factura.routes.js | Get one invoice by number |
GET | /factura-download/:number | factura.routes.js | Download invoice PDF (base64) |
POST | /factura | factura.routes.js | Create and validate a new invoice |
DELETE | /factura/:reference_code | factura.routes.js | Delete an unvalidated invoice |
GET | /get-data/:table | querys.routes.js | Select all rows from a table |
POST | /add-data/:table | querys.routes.js | Insert a row into a table |
POST | /get-join | querys.routes.js | Execute a raw JOIN query |
PUT / PATCH | /update-data/:table/:property/:value | querys.routes.js | Update rows matching a column value |
DELETE | /delete/:table/:property/:value | querys.routes.js | Delete rows matching a column value |
CORS Configuration
The backend applies a manual CORS middleware inmain.js before any route is handled. It uses a wildcard origin so that the frontend can be served from any host during development:
Authorization header is explicitly allowed because every Factus API proxy request includes a Bearer token forwarded from the client.
src/main.js — Full Source
src/main.js
