Overview
The Construction Backend API is a RESTful service built with Node.js, Express, and MongoDB. It provides endpoints for user authentication and quotation management for construction projects.This guide will help you set up and run the API locally on your machine. Make sure you have Node.js 16+ and MongoDB Atlas account ready.
Prerequisites
Before you begin, ensure you have the following installed:- Node.js (version 16 or higher)
- npm or yarn package manager
- MongoDB Atlas account (free tier available)
- A code editor (VS Code recommended)
Installation
Install dependencies
Install all required npm packages:This will install the following key dependencies:
express- Web frameworkmongoose- MongoDB object modelingbcryptjs- Password hashingcors- Cross-origin resource sharingdotenv- Environment variable management
Configure environment variables
Create a Add your MongoDB connection string and port configuration:
.env file in the root directory:Database connection
The API uses Mongoose to connect to MongoDB Atlas. Here’s how the connection is established:src/database/connection.js
src/database/config.js
Your first API call
Once the server is running, you can start making API calls. The API is available athttp://localhost:3000.
Test the connection
Verify the server is running:User authentication
Register a new user
Create a new user account:User schema details
User schema details
The user model includes the following fields:
document(String, required, unique) - User’s identification documentemail(String, required, unique) - User’s email addresspassword(String, required) - Hashed password using bcryptjsname(String, required) - User’s first namelast_name(String, required) - User’s last namecellphone(String, required) - User’s phone numberuser_type(String, required) - Type of user (e.g., contractor, admin)
Login
Authenticate an existing user:The password is never returned in responses. The API uses bcryptjs with a salt rounds of 10 for secure password hashing.
Quotation management
Create a quotation
Create a new construction project quotation:Get all quotations
Retrieve all stored quotations:Quotation schema details
Quotation schema details
The quotation model includes the following structure:Main fields:
factory(String, required) - Factory or construction site namefix(String, required) - Type of fix or construction workdescription_quotation(String, required) - Overall quotation descriptionsubtotal(Number, required) - Subtotal before taxes and feesunexpected(Number, required) - Unexpected expenses bufferiva(Number, required) - Tax amount (IVA)administratitive(Number, required) - Administrative feesutility(Number, required) - Profit margintotal_price(Number, required) - Final total price
description_sections(String, required) - Section descriptionsection_price(Number, required) - Total price for section
item_name(String, required) - Name of itemitem_description(String, required) - Item descriptionitem_total(Number, required) - Total cost for itemquantity(Number, required) - Quantity of itemsunity(String, required) - Unit of measurementitem_value(Number, required) - Value per unit
API routes overview
The API exposes the following routes:User routes (/api/user)
POST /api/user/register- Register a new userPOST /api/user/login- Authenticate a user
Quotation routes (/api/quotation)
GET /api/quotation/quotations- Get all quotationsPOST /api/quotation/register/quotation- Create a new quotation
Test route
GET /ruta-prueba- Health check endpoint
Error handling
The API returns standard HTTP status codes:200- Success201- Resource created successfully400- Bad request (validation error)401- Unauthorized (authentication failed)500- Internal server error
Next steps
Now that you have the API running, you can:- Explore the full API reference for detailed endpoint documentation
- Learn about advanced MongoDB queries with Mongoose
- Implement authentication tokens (JWT) for secure API access
- Deploy your API to production using Vercel or other platforms
The API is configured to work with Vercel serverless functions. The
app is exported as a module for serverless deployment.