A professional backend service scaffold built with Node.js, Express, and MongoDB.
This repository provides a clean backend foundation for a production-ready web application. It includes:
- Express server setup with modern middleware configuration
- MongoDB connection using Mongoose
- Environment-based configuration with
dotenv - CORS support, JSON parsing, URL-encoded body handling, cookie parsing, and static file serving
src/index.js- application bootstrap and server startup (loads./env, connects to MongoDB and starts the HTTP server)src/app.js- Express app instance, middleware registration, and static file servingsrc/db/index.js- MongoDB connection helper usingmongoose(connectDB)src/constants.js- shared constants (e.g. database namefirstdb)src/models/- Mongoose data modelsuser.model.js-Usermodel (username, email, fullName, avatar, password, refreshToken, watchHistory)video.model.js-Videomodel (file, thumbnail, title, description, duration, views, owner)
src/utils/- helper utilitiesApiError.js- custom error class to standardize API errorsApiResponse.js- standard API response wrapperasyncHandler.js- convenience wrapper for async route handlers
public/- static assets served by Expresssrc/controllers/- application controllers (add your route handlers here)src/middleware/- custom Express middlewarereadme.md- this filepackage.json- project metadata, dependencies andnpmscripts
src/index.js: Initializes environment variables from./env, callsconnectDB()and starts the HTTP server.src/app.js: Creates and exports the Expressappinstance, enables CORS (readsCORS_ORIGIN), JSON/body parsing, cookie parsing and servespublic/.src/db/index.js: Exports an asyncconnectDB()function that connects to${MONGODB_URI}/${DB_NAME}and logs the host.src/models/*: Mongoose schemas forUserandVideoincluding relations (owner,watchHistory) and utility methods (token generation, password checks).src/utils/*: Small helpers used across controllers to normalize error/response shapes and wrap async handlers.
- express
- cors
- cookie-parser
- dotenv
- mongoose
- bcrypt
- jsonwebtoken
- mongoose-aggregate-paginate-v2
- nodemon
- prettier
Create an env file at the project root (the project loads ./env via dotenv). Example:
PORT=8000
MONGODB_URI=mongodb://localhost:27017
CORS_ORIGIN=http://localhost:3000
ACCESS_TOKEN_SECRET=your-access-token-secret
REFRESH_TOKEN_SECRET=your-refresh-token-secret
ACCESS_TOKEN_EXPIRY=15m
REFRESH_TOKEN_EXPIRY=7d
Required values:
PORT- server listening portMONGODB_URI- base MongoDB URI (database name appended fromsrc/constants.js)CORS_ORIGIN- allowed origin for CORSACCESS_TOKEN_SECRET/REFRESH_TOKEN_SECRET- JWT secrets for token generationACCESS_TOKEN_EXPIRY/REFRESH_TOKEN_EXPIRY- token expiry configuration
Install dependencies:
npm installRun in development mode (uses nodemon and loads ./env):
npm run dev- Add controllers in
src/controllers/and wire routes in a routes file (e.g.,src/routes/) - Add middleware in
src/middleware/and register withapp.use()insrc/app.js - Add model methods or indexes inside
src/models/and import them in controllers
- The project uses ES modules (
type: "module"inpackage.json). - Static files are served from
public/. - The database name is defined in
src/constants.jsasfirstdb.
If you'd like, I can also generate a CONTRIBUTING.md and add examples for common routes (auth, upload, list videos).