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.
🔐 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
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
Node.js (v16 or higher)
PostgreSQL database
npm or yarn
Clone the repository and navigate to the project directory:
Install dependencies:
Set up environment variables:
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
Generate Prisma client:
Push the database schema:
The server will start on http://localhost:3000 (or the port specified in your .env file).
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
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
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
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
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
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
Method
Endpoint
Description
GET
/api/v1/health
Health check
GET
/
API information and documentation
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"
}'
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"
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"
}'
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"
}'
curl -X GET " http://localhost:3000/api/v1/posts/feed?limit=20" \
-H " Authorization: Bearer YOUR_ACCESS_TOKEN"
curl -X POST http://localhost:3000/api/v1/auth/refresh \
-H " Content-Type: application/json" \
-d ' {
"refreshToken": "YOUR_REFRESH_TOKEN"
}'
Push schema changes to database
Create and run migrations
Open Prisma Studio (database GUI)
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
Password hashing with bcrypt
JWT token-based authentication
Refresh token rotation
CORS protection
Helmet security headers
Input validation
SQL injection protection (via Prisma)
The API includes comprehensive error handling for:
Authentication errors
Validation errors
Database errors
JWT token errors
General server errors
Create new modules in src/modules/
Follow the pattern: router → controller → service
Add routes to src/router.js
Update database schema in prisma/schema.prisma if needed
Use async/await for asynchronous operations
Follow the existing error handling patterns
Add proper input validation
Include JSDoc comments for complex functions
MIT