Skip to content

Jeelislive/NOC-server

Repository files navigation

Nocapp Backend

Node.js Express MongoDB License: ISC

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 (see Backend/route/user.route.js)
  • Deployment target: Vercel (see Backend/vercel.json)

Features

  • 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

Tech Stack

  • 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

Getting Started

Prerequisites

  • Node.js >= 18
  • MongoDB database (Atlas or self‑hosted)
  • Cloudinary account (optional, for media)

Installation

# from repository root or Backend/ directory
cd Backend
npm install

Environment Variables

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 in index.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.

Running the Server (Development)

npm run dev

This runs the API with nodemon on the configured PORT (default: 4000).

Running the Server (Production‑like)

npm start

Note: start currently uses nodemon too. For real production, use node index.js or a process manager (PM2) on a server, or deploy to Vercel.


Scripts

npm run dev    # start with nodemon (hot reload)
npm start      # start (currently nodemon)

Consider adding lint/test scripts later for CI.


Project Structure

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)

API Overview

  • Base route prefix: /user
  • See route/user.route.js and controller/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 & Cookies

  • CORS is configured via constants/config.js and applied in index.js with app.use(cors(corsOptions)).
  • Cookies are parsed via cookie-parser.
  • Adjust allowed origins and credentials based on your frontend domain(s).

Deployment (Vercel)

This repo includes Backend/vercel.json for serverless deployment.

Basic steps:

  1. Create a new Vercel project targeting the Backend/ directory.
  2. Set environment variables in Vercel (same keys as your .env).
  3. Deploy. Vercel will use @vercel/node to build index.js and route /(.*) to it.

Alternative: Deploy on Railway/Render/EC2. For non‑serverless, run node index.js and expose PORT.


Security Notes

  • Do not commit .env.
  • Use strong values for JWT_SECRET and COOKIE_SECRET.
  • Set COOKIE_SECURE=true and SameSite=None when serving over HTTPS with cross‑site cookies.
  • Validate all inputs in controllers and/or middleware.

Troubleshooting

  • Mongo connection errors: verify MongoDBURI and IP access for your cluster.
  • CORS issues: check constants/config.js for allowed origins and credentials.
  • Cloudinary upload failures: ensure keys are set and account is provisioned.
  • 500 errors: see logs from middleware/error.js and controller stack traces.

License

ISC © Contributors. See package metadata.


Contributing

  • Use feature branches: git checkout -b feat/your-feature
  • Write clear commits (Conventional Commits recommended)
  • Open a PR with a description and test steps

Acknowledgements

  • Express, Mongoose, Cloudinary, Multer, Nodemailer, Passport, and the OSS community

About

Backend for NOC Issuence with NodeJs and Cloudinary to store documents

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published