π A modern Node.js + TypeScript REST API for the Connecto social platform
- π Project Structure
- π Authentication
- π Protected Endpoints
- π§ͺ Validation
- βοΈ .env Configuration
- π» Technologies Used
- π Deployment
- Β©οΈ License
- βοΈ Author
Below is the modular architecture of the backend project using
TypeScript,Express,Mongoose, and clean architecture principles.
π src
βοΈ config
Application configuration modules
| File | Description |
|---|---|
cors.config.ts |
Defines CORS policy to control cross-origin requests. |
db.config.ts |
Sets up and manages the MongoDB connection using Mongoose. |
index.ts |
Central entry point for all configuration modules for simplified imports. |
logger.config.ts |
Configures Winston logger for structured logging and integrates with Morgan. |
uploads.config.ts |
Defines Multer settings for secure and validated file uploads. |
π§ controllers
Business logic per feature
| Directory / File | Description |
|---|---|
auth/ |
Register/login logic |
comments/ |
CRUD for comments |
posts/ |
Post operations including likes and file handling |
users/ |
Profile & password updates |
index.ts |
Exports grouped controllers |
π¨ errors
Custom error classes and base handler
| File | Description |
|---|---|
base/HttpError.ts |
Generic HTTP error base class |
types/ClientErrors.ts |
4xx client error definitions |
types/ServerErrors.ts |
5xx server error definitions |
index.ts |
Central export for all error types |
π§± middlewares
Express middlewares
| File | Description |
|---|---|
auth.middleware.ts |
JWT authentication middleware |
errorHandler.middleware.ts |
Centralized error handling |
morganLogger.middleware.ts |
HTTP request logging using Morgan + Winston |
upload.middleware.ts |
Multer configuration for handling file uploads |
validate.middleware.ts |
Zod-based request schema validation |
index.ts |
Central export for middleware functions |
ποΈ models
Mongoose schemas
| File | Description |
|---|---|
User.ts, Posts.ts, Comments.ts |
MongoDB schema definitions using Mongoose |
index.ts |
Aggregates and re-exports models |
π£οΈ routes
Express routes per module
| File | Description |
|---|---|
auth.routes.ts |
Routes for authentication |
users.routes.ts |
Routes for user management |
posts.routes.ts |
Routes for post operations |
index.ts |
Combines and mounts all route modules |
π οΈ utils
General utility functions
| File | Description |
|---|---|
jwt.ts, argon.ts |
Utilities for authentication and hashing |
dbUtils.ts |
MongoDB-related helpers |
errorHandler.ts |
Error transformation and forwarding helpers |
serverUtils.ts |
Helpers for server setup, shutdown, etc. |
applyProtectedRoute.ts |
Adds JWT protection to route groups |
stackTrace.ts |
Parses and formats stack traces for debugging/logging |
responses.ts |
Standardized HTTP response formatting |
httpStatus.ts |
Central HTTP status code definitions |
index.ts |
Central utility re-export |
β validations
Zod schemas for validation
| File / Directory | Description |
|---|---|
auth/, comments/, posts, users |
Zod schema modules for request validation |
schemas.ts |
Central export of all Zod schema types |
βββ server.ts
POST /auth/signupβ Create a new userPOST /auth/loginβ Log in and receive JWT token
- JWT is stored in
Authorization: Bearer <token>header - Files access uses token in
?token=...query string (e.g.,<img src="/api/v1/web-embed/proxy?proxyUrl=https%3A%2F%2Fgithub.com%2Ffiles%2Fuser%2Favatar%2Ffoo.png%3Ftoken%3D...&mode=full" />) - Token settings (secret, expiry) are defined in
.env
GET /profileβ Get current user's profilePATCH /profileβ Update name, family name, or avatar (with file upload)PUT /passwordβ Change passwordDELETE /β Delete current user
POST /β Create new post (with optional file)GET /β Fetch all postsGET /:idβ Get single postPATCH /:id/likeβ Like/unlike a postPATCH /:idβ Update a post (and optionally update file)DELETE /:idβ Delete a postPOST /:id/commentsβ Add comment to a postGET /:id/commentsβ List comments on a post
GET /:idβ Get single commentPUT /:idβ Update comment contentDELETE /:idβ Delete comment
GET /user/avatar/:filename?token=...β Access user avatar (with token in URL)GET /post/:filename?token=...β Access post file (with token in URL)
All inputs are validated using Zod:
- Body (
req.body) - Params (
req.params) - Auth (
req.auth)
If validation fails, the API responds with a 400 Bad Request and details.
Make sure to create a .env file based on the example in the .env.example file
A range of modern and robust technologies was used to develop Connecto, a secure and scalable social platform. Below is a detailed breakdown:
| Technology | Description |
|---|---|
| Node.js β Runtime for backend JavaScript execution. | |
| TypeScript β Superset of JavaScript adding type safety and better tooling. | |
| Express.js β Fast, unopinionated, minimalist web framework for Node.js. | |
| JWT (jsonwebtoken) β Secure user authentication via JSON Web Tokens. | |
| Multer β Middleware for handling multipart/form-data for file uploads. | |
| Zod β Type-safe schema validation for input data (Zod + TypeScript). | |
| Mongoose β Elegant MongoDB object modeling for Node.js. | |
| Winston β Logging library for professional, structured logging. | |
| ESLint β Pluggable linter for maintaining code quality and enforcing standards. | |
| Prettier β Opinionated code formatter to ensure consistent styling. | |
| Visual Studio Code β Primary code editor used for development. | |
| Postman β API platform used for building, testing and documenting RESTful endpoints. |
This project is equipped with a streamlined development setup to ensure high code quality and consistency across environments.
- Nodemon β Automatically restarts the server on file changes during development.
- TypeScript β Enables static type checking for better reliability and maintainability.
- ESLint & Prettier β Enforces consistent code style and catches common errors.
- Environment Variables β Supports environment-specific configuration via
.envfiles.
Run the following scripts to manage development tasks efficiently:
npm run dev # Start the development server with Nodemon
npm run lint # Lint all source files using ESLint
npm run lint:fix # Automatically fix lint issues
npm run format # Format all files using Prettier
npm run format:check # Check if files are properly formatted (without modifying)π All Rights Reserved
This project and its entire source code are proprietary.
You are permitted to view the code only for informational or educational insight.
You may not:
- Reuse or copy any part of this codebase
- Modify, adapt, or build upon this project
- Distribute or make it publicly available
- Use it for personal, academic, or commercial projects
Unauthorized use will be considered a violation of copyright law.
For special permissions, contact the author at :
|
MasterCodeDevelop |
Passionate Full-Stack Developer. π Connect with me on GitHub π‘ Contributions, issues and feature requests are welcome! |