Skip to content

A RESTful backend API built with Node.js, Express, and MongoDB

Notifications You must be signed in to change notification settings

fetrafaneva/SocialApp_API_REST

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Social-API REST

A RESTful backend API built with Node.js, Express, and MongoDB. This API powers a social media application with users, posts, comments, replies, and private messaging.

Node.js Version React MongoDB

Overview

Social-API REST is a scalable and secure REST API designed to handle the core features of a social platform. It follows best practices in API design, authentication, data modeling, and middleware usage.

The API is frontend-agnostic and can be consumed by any client (React, Flutter, mobile apps, etc.).

Key Features

User Management

  • Secure registration and login with JWT
  • Password hashing with bcrypt
  • Profile updates

Posts

  • Create, read, update, delete posts
  • Comments and threaded replies
  • Likes system (optional)

Messaging

  • Send private messages
  • Real-time notifications with Socket.io
  • Mark messages as read
  • Count unread messages
  • Update and delete messages

Notifications

  • Track user notifications
  • Mark notifications as read

Security

  • JWT authentication
  • Middleware for protected routes
  • Input validation
  • Environment variables for sensitive data

Additional Features

  • Modular project structure
  • Clean code with controllers, middleware, and routes
  • Ready for deployment (Heroku, Render, Railway, etc.)

Tech Stack

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • JWT (JSON Web Tokens)
  • bcrypt
  • dotenv
  • cors

Project Structure

backend/
├── config/
│   └── db.js                         # MongoDB connection
│
├── controllers/
│   ├── auth.controller.js            # Authentication logic
│   ├── user.controller.js            # User-related actions
│   ├── post.controller.js            # Posts logic
│   ├── notification.controller.js    # Notification logic
│   ├── message.controller.js         # Messaging logic
│
├── middleware/
│   ├── auth.middleware.js            # JWT authentication
│   ├── upload.middleware.js          # Config upload file
│
├── models/
│   ├── user.model.js                 # user shema
│   ├── notification.model.js         # notification shema
│   ├── message.model.js              # message shema
│
├── routes/
│   ├── auth.routes.js
│   ├── notification.routes.js
│   ├── post.routes.js
│
├── utils/
│   └──createNotification.js
│
├── server.js                         # App entry point
├── package.json
└── .env

Installation

Prerequisites

  • Node.js (v18 or higher)
  • MongoDB (local instance or MongoDB Atlas)

Steps

  1. Clone the repository:
    git clone https://github.com/fetrafanevacontent-api-backend.git
    cd backend
    
  2. Install dependencies:
    cd backend
    npm install
    
  3. Configure environment variables:
  • Create .env in /server:
    PORT=your_port
    
    MONGO_URI=your_mongodb_URI
    
    JWT_SECRET=your_secure_jwt_secret
    
  1. Start the application:
    npm run server (with nodemon)
    

API Endpoints

Authentication

Method Endpoint Description Authentication Required
POST /api/auth/register Register a new user No
POST /api/auth/login User login No
GET /api/auth/logout User logout yes

Posts

Method Endpoint Description Authentication Required
POST /api/post Create post yes
GET /api/post Get all post yes
PUT /api/post/:id Update post yes
DELETE /api/auth/:id Delete post yes

Comments & Replies

Method Endpoint Description Authentication Required
POST /api/post/:id/comment Get message yes
POST /api/post/:postId/comment/:commentId/reply Reply to comment yes
PATCH /api/post/:postId/comment/:commentId/reply/:replyId Uptdate reply yes
DELETE /api/post/:postId/comment/:commentId/reply/:replyId Delete reply yes

Messages

Method Endpoint Description Authentication Required
POST /api/message/ Send message yes
GET /api/message/inbox Comment a post yes
GET /api/message/unread/count Count unread message yes
GET /api/message/conversations GET all conversations yes
GET /api/message/conversation/:userId Get users conversation yes
PATCH /api/message/conversations/:userId/read Mark conversation as read yes
PATCH /api/message/:id/read Mark message as read yes
PUT /api/message/:id Update message yes
DELETE /api/message/:id Delete message yes

Notification

Method Endpoint Description Authentication Required
GET /api/notification/ Get notification yes
PATCH /api/notification/:id/read Mark notiication as read yes

Security Practices

  • Passwords hashed with bcrypt
  • JWT authentication with middleware
  • Protected routes for sensitive actions
  • Input validation on backend
  • Environment variables for sensitive keys
  • CORS configuration for frontend only

Testing

  • Use Postman or Insomnia to test endpoints
  • Use Postman or Insomnia to test endpoints

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

If this project helps you, consider starring the repository!

About

A RESTful backend API built with Node.js, Express, and MongoDB

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published