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:Configure each variable
Variable Descriptions
| Variable | Description | Example |
|---|---|---|
APP_NAME | Subject line for emails sent by the API | "Stock Management System" |
PORT | Port number where the server will run | 3000 |
MONGO_URI | MongoDB Atlas connection string | See Database Connection |
JWT_SECRET | Secret key for JWT token generation | "my-super-secret-key-123" |
EMAIL_USER | Email address to receive contact form messages | "[email protected]" |
EMAIL_PASS | Password or app-specific key for email account | "your-app-password" |
RESEND_API_KEY | API key from Resend.com | "re_123abc..." |
Installing Dependencies
StockAPI uses several npm packages that need to be installed before running the application.Core Dependencies
The application uses the following npm packages (frompackage.json):
Package Purposes
- express: Web framework for building the API
- mongoose: MongoDB object modeling for Node.js
- dotenv: Loads environment variables from
.envfile - 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: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 thePORT variable in your .env file:
Missing Dependencies
If you encounter module not found errors, ensure all dependencies are installed:Environment Variables Not Loading
Make sure:- Your
.envfile is in the root directory - There are no spaces around the
=sign in your.envfile - You restart the server after changing environment variables
Next Steps
- Database Connection - Set up MongoDB Atlas
- Docker Deployment - Deploy using Docker containers