How ralitsavoronevska/rest-api-with-nodejs works
Purpose and runtime
This repository is a Node.js/Express REST API for a basic shop domain: user authentication, product inventory, and orders. It follows MVC-style separation over MongoDB Atlas via Mongoose. The process starts in server.js, with Express application assembly in app.js.
HTTP application boundary
app.js configures request parsing, logging, static/upload handling, route mounting, and centralized error responses. It is the boundary between HTTP clients and domain handlers; requests flow from Express middleware to resource routes, then controllers and Mongoose models.
Resource routing
Three route modules expose resource-oriented endpoints:
• Users: signup/login through api/routes/user.js
• Products: collection and item CRUD through api/routes/products.js
• Orders: collection and item operations through api/routes/orders.js
Routes define public versus protected operations and delegate business/request handling rather than directly persisting data.
Authentication and authorization
User registration and login are implemented in api/controllers/user.js, backed by api/models/user.js. Passwords are hashed with bcrypt; successful login produces JWTs. Protected product/order endpoints pass through api/middleware/check-auth.js, which validates the bearer token before controller execution.
Product inventory and file uploads
Product API behavior is concentrated in api/controllers/products.js, with persistence schema/model logic in api/models/product.js. Product creation supports multipart image uploads through Multer; uploaded assets are stored under uploads, while product responses expose product resources and associated image paths. Public reads provide product listing/detail access; mutations require authentication.
Orders and product references
api/controllers/orders.js implements order creation, retrieval, and deletion against api/models/order.js. Order processing sits at the integrity boundary between orders and products: controllers use Mongoose to resolve product records and persist order data, returning resource links/status responses. Protected access prevents unauthenticated order operations.
Infrastructure and dependencies
The service runs on Node.js and Express, using MongoDB Atlas as its persistence service and Mongoose as the ODM. Cross-cutting middleware includes body parsing, Morgan request logging, Multer upload processing, and async error propagation. Nodemon supports local restart-on-change development; Postman is the documented API client/testing workflow.
Main entry points: app.js Express app · check-auth.js JWT auth middleware · controllers/products.js Product controller · routes/products.js Product routes
Components 14
Runtime
- Server entry · Starts the API process.
server.js
HTTP API
- Express app · Configures middleware, routes, static files, and error responses.
app.js - User routes · Exposes signup and login.
api/routes/user.js - Product routes · Exposes public reads and protected mutations.
api/routes/products.js - Order routes · Exposes protected order operations.
api/routes/orders.js - JWT auth middleware · Validates bearer tokens before protected handlers.
api/middleware/check-auth.js
Domain
- User controller · Hashes passwords and issues JWTs.
api/controllers/user.js - Product controller · Handles product CRUD and image-backed creation.
api/controllers/products.js - Order controller · Resolves products and persists orders.
api/controllers/orders.js
Data & assets
- User model
api/models/user.js - Product model
api/models/product.js - Order model · Stores order data with product references.
api/models/order.js - Upload assets · Stores Multer-managed product images.
uploads - MongoDB Atlas · Persistent datastore accessed through Mongoose.
Connections 18
- Server entry → Express app: starts
- Express app → User routes: mounts
- Express app → Product routes: mounts
- Express app → Order routes: mounts
- User routes → User controller: delegates
- Product routes → JWT auth middleware: protects mutations
- Order routes → JWT auth middleware: protects operations
- JWT auth middleware → Product controller: authorizes
- JWT auth middleware → Order controller: authorizes
- Product routes → Product controller: public reads
- User controller → User model: uses
- Product controller → Product model: uses
- Product controller → Upload assets: stores images
- Order controller → Order model: uses
- Order controller → Product model: resolves product
- User model → MongoDB Atlas: persists via Mongoose
- Product model → MongoDB Atlas: persists via Mongoose
- Order model → MongoDB Atlas: persists via Mongoose