Skip to main content

Overview

StockAPI requires specific environment variables and Node.js dependencies to function properly. This guide will walk you through setting up your development environment.

Prerequisites

  • Node.js 20 or higher
  • npm (Node Package Manager)
  • A MongoDB Atlas account (for database connection)
  • A Resend account (for email functionality)

Environment Variables

StockAPI uses the following environment variables that must be configured before running the application:
1

Create .env file

Create a .env file in the root directory of your project:
touch .env
2

Add required variables

Add the following environment variables to your .env file:
.env
# Application Configuration
APP_NAME="Your App Name"
PORT=3000

# MongoDB Connection
MONGO_URI="mongodb+srv://username:password@cluster.mongodb.net/database"

# JWT Authentication
JWT_SECRET="your-secret-key-here"

# Email Configuration
EMAIL_USER="your-email@example.com"
EMAIL_PASS="your-email-password-or-app-key"

# Resend API (for email sending)
RESEND_API_KEY="re_your_api_key_here"
3

Configure each variable

Variable Descriptions

VariableDescriptionExample
APP_NAMESubject line for emails sent by the API"Stock Management System"
PORTPort number where the server will run3000
MONGO_URIMongoDB Atlas connection stringSee Database Connection
JWT_SECRETSecret key for JWT token generation"my-super-secret-key-123"
EMAIL_USEREmail address to receive contact form messages"support@example.com"
EMAIL_PASSPassword or app-specific key for email account"your-app-password"
RESEND_API_KEYAPI key from Resend.com"re_123abc..."
Never commit your .env file to version control. Make sure .env is listed in your .gitignore file to prevent accidentally exposing sensitive credentials.

Installing Dependencies

StockAPI uses several npm packages that need to be installed before running the application.
1

Install production dependencies

Run the following command to install all required dependencies:
npm install
This will install the following packages:

Core Dependencies

The application uses the following npm packages (from package.json):
{
  "dependencies": {
    "bcryptjs": "^3.0.2",
    "body-parser": "^2.2.0",
    "cors": "^2.8.5",
    "dotenv": "^17.2.3",
    "express": "^5.1.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.19.2",
    "nodemailer": "^7.0.10",
    "resend": "^6.4.2"
  },
  "devDependencies": {
    "nodemon": "^3.1.10"
  }
}

Package Purposes

  • express: Web framework for building the API
  • mongoose: MongoDB object modeling for Node.js
  • dotenv: Loads environment variables from .env file
  • jsonwebtoken: JWT token generation and validation for authentication
  • bcryptjs: Password hashing for secure user credential storage
  • body-parser: Middleware for parsing incoming request bodies
  • cors: Middleware for handling Cross-Origin Resource Sharing
  • nodemailer: Email sending functionality
  • resend: Modern email API service integration
  • nodemon (dev): Automatically restarts server during development

Running the Application

Once your environment is configured and dependencies are installed, you can start the application:
npm start
If everything is configured correctly, you should see:
 Conexión a MongoDB Atlas establecida correctamente
 Servidor funcionando en el puerto 3000
The application will not start if it cannot connect to MongoDB. Make sure your MONGO_URI is correctly configured before running the app.

Troubleshooting

Port Already in Use

If port 3000 is already in use, change the PORT variable in your .env file:
PORT=3001

Missing Dependencies

If you encounter module not found errors, ensure all dependencies are installed:
rm -rf node_modules package-lock.json
npm install

Environment Variables Not Loading

Make sure:
  • Your .env file is in the root directory
  • There are no spaces around the = sign in your .env file
  • You restart the server after changing environment variables

Next Steps