Skip to content

About

Backend for the application

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Weavely Social Network Backend

A comprehensive Node.js social network backend API built with Express.js, PostgreSQL, and Prisma ORM featuring JWT authentication, user management, friendships, posts, groups, and intelligent recommendations.

Features

  • 🔐 JWT-based authentication with refresh tokens
  • 🗄️ PostgreSQL database with Prisma ORM
  • 🛡️ Security middleware (Helmet, CORS, Rate Limiting)
  • 👥 User management with profiles and privacy settings
  • 🤝 Friendship system with requests and management
  • 📝 Posts with likes, comments, and media support
  • 👥 Groups with membership and administration
  • 🎯 Intelligent recommendations for friends, groups, and posts
  • 📊 Cursor-based pagination for feeds
  • 🔍 Search functionality across users and groups
  • 🛡️ Input validation and sanitization
  • 📈 Rate limiting and security best practices

Project Structure

src/
├── app.js                 # Express app entry point
├── router.js              # Main application router
├── config/
│   └── db.js             # Prisma client configuration
├── middleware/
│   └── auth.js           # JWT authentication middleware
└── modules/
    └── auth/
        ├── auth.router.js    # Auth routes
        ├── auth.controller.js # Auth request handlers
        └── auth.service.js   # Auth business logic

prisma/
└── schema.prisma         # Database schema

Prerequisites

  • Node.js (v16 or higher)
  • PostgreSQL database
  • npm or yarn

Installation

  1. Clone the repository and navigate to the project directory:
cd backend
  1. Install dependencies:
npm install
  1. Set up environment variables:
cp env.example .env
  1. Update the .env file with your database credentials and JWT secrets:
DATABASE_URL="postgresql://username:password@localhost:5432/weavely_db"
JWT_SECRET="your-super-secret-jwt-key-here"
JWT_REFRESH_SECRET="your-super-secret-refresh-key-here"
PORT=3000
NODE_ENV=development
  1. Generate Prisma client:
npm run db:generate
  1. Push the database schema:
npm run db:push

Running the Application

Development

npm run dev

Production

npm start

The server will start on http://localhost:3000 (or the port specified in your .env file).

API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /api/v1/auth/register Register a new user No
POST /api/v1/auth/login Login user No
POST /api/v1/auth/refresh Refresh access token No
POST /api/v1/auth/logout Logout user No

Users

Method Endpoint Description Auth Required
GET /api/v1/users Get users with search Yes
GET /api/v1/users/:id Get user profile Yes
PUT /api/v1/users/:id Update user profile Yes
GET /api/v1/users/:id/friends Get user's friends Yes
GET /api/v1/users/:id/friends-of-friends Get friends of friends Yes

Friendships

Method Endpoint Description Auth Required
POST /api/v1/friends/request Send friend request Yes
POST /api/v1/friends/accept Accept friend request Yes
POST /api/v1/friends/reject Reject friend request Yes
DELETE /api/v1/friends/remove Remove friend Yes

Posts

Method Endpoint Description Auth Required
POST /api/v1/posts Create a post Yes
GET /api/v1/posts Get all posts Yes
GET /api/v1/posts/feed Get personalized feed Yes
GET /api/v1/posts/:id Get post details Yes
POST /api/v1/posts/:id/like Like a post Yes
DELETE /api/v1/posts/:id/like Unlike a post Yes
POST /api/v1/posts/:id/comment Comment on post Yes

Groups

Method Endpoint Description Auth Required
POST /api/v1/groups Create a group Yes
GET /api/v1/groups Get groups with search Yes
GET /api/v1/groups/:id Get group details Yes
PUT /api/v1/groups/:id Update group Yes
DELETE /api/v1/groups/:id Delete group Yes
POST /api/v1/groups/:id/join Join group Yes
POST /api/v1/groups/:id/leave Leave group Yes
GET /api/v1/groups/:id/members Get group members Yes

Recommendations

Method Endpoint Description Auth Required
GET /api/v1/recommendations/friends/:userId Get friend recommendations Yes
GET /api/v1/recommendations/groups/:userId Get group recommendations Yes
GET /api/v1/recommendations/posts/:userId Get post recommendations Yes

Other

Method Endpoint Description
GET /api/v1/health Health check
GET / API information and documentation

API Usage Examples

Register a new user

curl -X POST http://localhost:3000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "email": "john@example.com",
    "password": "P@ssw0rd123"
  }'

Login

curl -X POST http://localhost:3000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "P@ssw0rd123"
  }'

Get user profile (requires authentication)

curl -X GET http://localhost:3000/api/v1/users/USER_ID \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Create a post

curl -X POST http://localhost:3000/api/v1/posts \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -d '{
    "content": "Hello, world! This is my first post.",
    "mediaUrl": "https://example.com/image.jpg"
  }'

Send friend request

curl -X POST http://localhost:3000/api/v1/friends/request \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -d '{
    "receiverId": "FRIEND_USER_ID"
  }'

Get personalized feed

curl -X GET "http://localhost:3000/api/v1/posts/feed?limit=20" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Refresh token

curl -X POST http://localhost:3000/api/v1/auth/refresh \
  -H "Content-Type: application/json" \
  -d '{
    "refreshToken": "YOUR_REFRESH_TOKEN"
  }'

Database Management

Generate Prisma client

npm run db:generate

Push schema changes to database

npm run db:push

Create and run migrations

npm run db:migrate

Open Prisma Studio (database GUI)

npm run db:studio

Environment Variables

Variable Description Default
DATABASE_URL PostgreSQL connection string Required
JWT_SECRET Secret key for access tokens Required
JWT_REFRESH_SECRET Secret key for refresh tokens Required
PORT Server port 3000
NODE_ENV Environment mode development

Security Features

  • Password hashing with bcrypt
  • JWT token-based authentication
  • Refresh token rotation
  • CORS protection
  • Helmet security headers
  • Input validation
  • SQL injection protection (via Prisma)

Error Handling

The API includes comprehensive error handling for:

  • Authentication errors
  • Validation errors
  • Database errors
  • JWT token errors
  • General server errors

Development

Adding New Features

  1. Create new modules in src/modules/
  2. Follow the pattern: router → controller → service
  3. Add routes to src/router.js
  4. Update database schema in prisma/schema.prisma if needed

Code Style

  • Use async/await for asynchronous operations
  • Follow the existing error handling patterns
  • Add proper input validation
  • Include JSDoc comments for complex functions

License

MIT

About

Backend for the application

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages