Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/despacho-backend/llms.txt

Use this file to discover all available pages before exploring further.

This guide walks you through cloning the repository, wiring up the required environment variables, starting the development server, registering a lawyer account, and making an authenticated request to list all reservations — everything you need to verify that the API is working end-to-end on your machine.
1

Prerequisites

Before you begin, make sure the following are available in your environment:
  • Node.js 18 or later — the project uses native ES modules and performance.now() at startup. Run node -v to confirm.
  • npm — bundled with Node.js. Run npm -v to confirm.
  • A MongoDB instance — either a locally running mongod process or a free MongoDB Atlas cluster. You will need the connection URI in the next step.
2

Clone and install dependencies

Clone the repository and install all npm dependencies:
git clone https://github.com/fredy-rizo/despacho-backend.git
cd despacho-backend
npm install
npm will install Express, Mongoose, jsonwebtoken, bcrypt, Socket.io, multer, dotenv, morgan, and the nodemon dev dependency.
3

Configure environment variables

Despacho Backend reads all configuration from a .env file in the project root via dotenv. Create the file now:
touch .env
Then populate it with the three required variables:
# .env
PORT=3000
SECRET=your-very-long-random-secret-string-here
MONGODB_URL=mongodb://localhost:27017/despacho
VariableDescription
PORTThe port the Express HTTP server will listen on. Defaults to 3000 if omitted.
SECRETThe signing secret for JWTs. Must be a long, random string — see Configuration for generation tips.
MONGODB_URLFull MongoDB connection URI. Use mongodb://localhost:27017/despacho for a local instance or your Atlas connection string.
Never commit .env to source control. Add it to .gitignore immediately. Leaking SECRET allows anyone to forge valid JWTs and gain full access to all protected routes.
4

Start the development server

Run the dev script, which uses nodemon to restart on file changes:
npm run dev
If your environment is configured correctly you will see:
Server-startup → 4.21.ms
Server on port → 3000 ✅
Connected to Mongoose ✅
If you see Error connecting to Mongoose ❌ instead, double-check that your MONGODB_URL is reachable and that mongod is running (or your Atlas cluster is active).
5

Create a lawyer account

With the server running, register the first lawyer account by posting to POST /api/lawyer/user/create. The endpoint accepts email and password in the JSON body, hashes the password with bcrypt, and saves the document with the Admin role.
curl -X POST http://localhost:3000/api/lawyer/user/create \
  -H "Content-Type: application/json" \
  -d '{"email": "abogado@despacho.com", "password": "MyStr0ngP@ss"}'
A successful response looks like:
{
  "msj": "Cuenta creada correctamente",
  "status": true,
  "new_user": {
    "_id": "6741a2f3e4b0c1d2e3f40001",
    "token": "",
    "email": "abogado@despacho.com",
    "password": "$2b$10$...",
    "role": [{ "name": "Admin", "value": "1" }],
    "createdAt": "2024-11-23T10:00:00.000Z",
    "updatedAt": "2024-11-23T10:00:00.000Z"
  }
}
If the email is already registered the API returns HTTP 203 with "status": false.
6

Log in and retrieve your JWT

Call POST /api/lawyer/user/login with the same credentials to receive a signed JWT valid for 365 days.
curl -X POST http://localhost:3000/api/lawyer/user/login \
  -H "Content-Type: application/json" \
  -d '{"email": "abogado@despacho.com", "password": "MyStr0ngP@ss"}'
The response includes the JWT in the token field:
{
  "msj": "Bienvenido!",
  "status": true,
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NzQxYTJmM2U0YjBjMWQyZTNmNDAwMDEiLCJlbWFpbCI6ImFib2dhZG9AZGVzcGFjaG8uY29tIiwicm9sZSI6W3sibmFtZSI6IkFkbWluIiwidmFsdWUiOiIxIn1dLCJpYXQiOjE3MzIzNTIwMDAsImV4cCI6MTc2Mzg4ODAwMH0.SIGNATURE",
  "user": {
    "_id": "6741a2f3e4b0c1d2e3f40001",
    "email": "abogado@despacho.com",
    "role": [{ "name": "Admin", "value": "1" }]
  }
}
Copy the value of token and keep it handy — you will pass it as a Bearer token in the Authorization header of every subsequent request to a protected endpoint. The token is also stored on the LawyerUser document in MongoDB for reference.
7

Call a protected endpoint

Every reservation listing and management route is protected by the Token middleware, which expects an Authorization: Bearer <token> header. Use the token from the previous step to list all reservations:
curl -X POST http://localhost:3000/api/form/reserve/list/reservation-all \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -d '{}'
A successful response returns a paginated list of all reservations (empty array if none have been submitted yet) along with pagination metadata:
{
  "msj": "Cargando todas las reservas",
  "status": true,
  "data": [],
  "pagination": {
    "pag": null,
    "perpage": 10,
    "pags": 0
  }
}
If you omit the Authorization header, the Token middleware returns HTTP 401 {"msj": "Sin autorizacion", "status": false}. If the token is expired or invalid, it returns HTTP 403.

Build docs developers (and LLMs) love