This is the backend for Shoppy, an e-commerce platform. It is built with Node.js, TypeScript, and Express, and provides RESTful APIs for user authentication, product management, order processing, and more.
- User authentication (JWT-based)
- Product CRUD operations
- Category management
- Order management
- File uploads (Cloudinary integration)
- Error handling and logging
- Email notifications
src/
config/ # Database and app configuration
controllers/ # Route handlers for business logic
helper/ # Utility functions (JWT, email, cloudinary)
middlewares/ # Express middlewares (auth, error, logger, upload, validation)
models/ # Mongoose schemas for data models
routes/ # API route definitions
services/ # Service layer for business logic
types/ # TypeScript type definitions
utils/ # Utility functions
public/ # Static files (images, etc.)
api/ # API entry point
- Node.js (v18+ recommended)
- npm
- MongoDB (local or Atlas)
- Cloudinary account (for image uploads)
- Clone the repository:
git clone https://github.com/abdullahriad-alghamdi/Shoppy-backEnd.git cd Shoppy-backEnd - Install dependencies:
npm install
- Configure environment variables:
Create a
.envfile in the root directory with the following variables:MONGODB_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name CLOUDINARY_API_KEY=your_cloudinary_api_key CLOUDINARY_API_SECRET=your_cloudinary_api_secret EMAIL_USER=your_email_address EMAIL_PASS=your_email_password
- Start the server:
The server will run on the port specified in your configuration (default: 3000).
npm start
If you are running the project locally, make sure to:
-
Set up your own
.envfile:- Use your local MongoDB connection string (e.g.,
mongodb://localhost:27017/shoppy) - Set a secure value for
JWT_SECRET(any random string) - Use your own Cloudinary credentials, or comment out Cloudinary-related code if you do not need image uploads
- For email features, use a test email account or disable email sending in development
- Use your local MongoDB connection string (e.g.,
-
Check the port configuration:
- By default, the server runs on port 3000. You can change this in your configuration or
.envfile if needed.
- By default, the server runs on port 3000. You can change this in your configuration or
-
Review any hardcoded URLs or paths:
- Make sure any URLs (e.g., for API calls, static files) point to your local server during development.
-
Install all dependencies:
- Run
npm installto ensure all required packages are available.
- Run
-
Start MongoDB locally:
- If using a local database, ensure MongoDB is running before starting the server.
To enable your frontend to communicate with the backend during local development, set the CORS_ORIGIN variable in your .env file to the URL of your frontend (e.g., http://localhost:3003).
This value is used to set the Access-Control-Allow-Origin header, which controls which origins are allowed to access your API. If you change your frontend's port or domain, update CORS_ORIGIN accordingly.
Example:
CORS_ORIGIN=http://localhost:3003This ensures your backend accepts requests from your local frontend. No code changes are needed—just update the value in .env.
npm run devnpm run build/api/auth- Authentication routes (register, login, etc.)/api/products- Product CRUD operations/api/categories- Category management/api/orders- Order management/api/users- User management
GET /api/products[
{
"_id": "...",
"name": "Product Name",
"price": 100,
"category": "Category Name"
// ...other fields
}
]Errors are returned in JSON format with an appropriate HTTP status code:
{
"error": "Resource not found"
}When deploying to Vercel, set the CORS_ORIGIN environment variable in your Vercel project settings to match your production frontend URL (e.g., https://your-frontend-domain.com).
This will set the Access-Control-Allow-Origin header for your deployed backend, just as it does locally. If you need to allow multiple origins, update your backend code to support that logic.
You can also configure custom headers in your vercel.json file if needed. Example:
{
"headers": [
{
"source": "/api/(.*)",
"headers": [
{ "key": "Access-Control-Allow-Origin", "value": "https://your-frontend-domain.com" }
]
}
]
}This ensures your backend on Vercel only accepts requests from your specified frontend domain.
- The project includes a
vercel.jsonfor deployment on Vercel. - Configure environment variables in your deployment platform.
- Fork the repository
- Create a new branch (
git checkout -b feature-branch) - Commit your changes
- Push to your branch and open a pull request
MIT
For questions or support, open an issue on GitHub or contact me.