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
1
Navigate to the backend directory
All backend source code lives under
bc-v1/. Move into it before running any commands.2
Install dependencies
Install the five production dependencies with npm:The following packages will be installed:
3
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.4
Start the development server
Run the You should see:
dev script to start the server with hot reload:Available npm Scripts
Thepackage.json exposes five scripts:
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
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
