A minimal, production‑ready REST API built with Express and Mongoose. Includes auth, file uploads, email, and cloud media support.
- Base server file:
Backend/index.js
- API root:
/user
(seeBackend/route/user.route.js
) - Deployment target: Vercel (see
Backend/vercel.json
)
- Express API with CORS, cookies, and JSON body parsing
- MongoDB via Mongoose
- Authentication helpers (JWT, bcrypt)
- Cloudinary integration for media storage
- File uploads via Multer
- Email sending (Nodemailer)
- Centralized error handling middleware
- Configurable CORS via
constants/config.js
- Ready for Vercel serverless deployment
- Runtime: Node.js 18+
- Framework: Express 4
- Database: MongoDB (Mongoose 8)
- Auth: JWT, bcrypt, optional Google OAuth (Passport)
- Uploads/Media: Multer, Cloudinary
- Mailer: Nodemailer
- Utilities: uuid, cookie‑parser, dotenv
- Node.js >= 18
- MongoDB database (Atlas or self‑hosted)
- Cloudinary account (optional, for media)
# from repository root or Backend/ directory
cd Backend
npm install
Create a .env
file in Backend/
with the following keys. Do not commit secrets.
# Server
PORT=4000
# Database
MongoDBURI=mongodb+srv://<username>:<password>@<cluster>/<db>?retryWrites=true&w=majority
# JWT / Auth
JWT_SECRET=replace_me
JWT_EXPIRES_IN=7d
# Cookies
COOKIE_SECRET=replace_me
COOKIE_SECURE=false
# Cloudinary
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# Mail (optional)
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_SECURE=false
SMTP_USER=your_user
SMTP_PASS=your_password
[email protected]
# OAuth (optional)
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=
- MongoDB URI must be set as
MongoDBURI
(used inindex.js
). - Cloudinary keys are only required if you use media uploads.
- OAuth and SMTP settings are optional and used when those features are enabled.
Tip: Add a
.env.example
file (git‑tracked) for collaborators. I can generate one if you’d like.
npm run dev
This runs the API with nodemon
on the configured PORT
(default: 4000).
npm start
Note:
start
currently usesnodemon
too. For real production, usenode index.js
or a process manager (PM2) on a server, or deploy to Vercel.
npm run dev # start with nodemon (hot reload)
npm start # start (currently nodemon)
Consider adding lint/test scripts later for CI.
Backend/
├─ index.js # Express app entrypoint
├─ vercel.json # Vercel config for serverless deploy
├─ .env # environment variables (gitignored)
├─ constants/
│ └─ config.js # CORS and other constants
├─ controller/
│ └─ user.controller.js # user/business logic
├─ middleware/
│ ├─ auth.middleware.js # auth guards
│ └─ error.js # error handler middleware
├─ model/
│ ├─ user.model.js # user schema
│ └─ checklist.model.js # checklist schema
├─ route/
│ └─ user.route.js # user routes (mounted at /user)
├─ utils/
│ ├─ features.js # helpers/utilities
│ ├─ multer.js # upload config
│ └─ utility.js # misc utilities
└─ OAuth/
└─ googleHandler.jsx # Google OAuth handler (if used)
- Base route prefix:
/user
- See
route/user.route.js
andcontroller/user.controller.js
for exact endpoints and payloads.
Common patterns:
- Authenticated routes expect a Bearer token or cookie depending on your middleware.
- Validation and error handling flow through
middleware/error.js
.
If you want, I can generate endpoint docs from your routers/controllers into this README.
- CORS is configured via
constants/config.js
and applied inindex.js
withapp.use(cors(corsOptions))
. - Cookies are parsed via
cookie-parser
. - Adjust allowed origins and credentials based on your frontend domain(s).
This repo includes Backend/vercel.json
for serverless deployment.
Basic steps:
- Create a new Vercel project targeting the
Backend/
directory. - Set environment variables in Vercel (same keys as your
.env
). - Deploy. Vercel will use
@vercel/node
to buildindex.js
and route/(.*)
to it.
Alternative: Deploy on Railway/Render/EC2. For non‑serverless, run
node index.js
and exposePORT
.
- Do not commit
.env
. - Use strong values for
JWT_SECRET
andCOOKIE_SECRET
. - Set
COOKIE_SECURE=true
andSameSite=None
when serving over HTTPS with cross‑site cookies. - Validate all inputs in controllers and/or middleware.
- Mongo connection errors: verify
MongoDBURI
and IP access for your cluster. - CORS issues: check
constants/config.js
for allowed origins andcredentials
. - Cloudinary upload failures: ensure keys are set and account is provisioned.
- 500 errors: see logs from
middleware/error.js
and controller stack traces.
ISC © Contributors. See package metadata.
- Use feature branches:
git checkout -b feat/your-feature
- Write clear commits (Conventional Commits recommended)
- Open a PR with a description and test steps
- Express, Mongoose, Cloudinary, Multer, Nodemailer, Passport, and the OSS community