Welcome to the Social Media Backend! This is the backend API for the Social Media Full Stack App. It is built with Node.js and Express.js, and uses PostgreSQL for storing user data, posts, and other social features.
- Node.js – A JavaScript runtime for the server.
- Express.js – A web framework for Node.js.
- PostgreSQL – Relational database for storing user data and posts.
- Sequelize – SQL ORM for Node.js with PostgreSQL support.
- JWT (JSON Web Token) – For user authentication and authorization.
- Bcrypt.js – For hashing user passwords.
- Multer – For handling file uploads (profile pictures, post images).
- Helmet – For security headers and CORS configuration.
- User Authentication: Secure login and registration with JWT.
- Post Management: Create, view, update, and delete posts.
- User Profile: Store and update user profile information.
- Follow/Unfollow System: Users can follow and unfollow each other.
- Like/Unlike Posts: Interactive engagement with posts.
- Timeline Feed: View posts from followed users.
- Image Upload: Profile pictures, cover photos, and post images.
- CORS Support: Configured to accept requests from the frontend.
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
Download and install from: https://www.postgresql.org/download/
psql -U postgres
CREATE DATABASE socialmedia_db;
\qcd Social-Media-Backend
npm installCreate a .env file:
DB_NAME=socialmedia_db
DB_USER=postgres
DB_PASSWORD=your_password_here
DB_HOST=localhost
DB_PORT=5432
PORT=8800
JWT_SECRET=your_jwt_secret_keynpm startExpected output:
✅ PostgreSQL connection established successfully
✅ Database synchronized successfully
Server is running on port 8800
POST /api/auth/register- Register new userPOST /api/auth/login- Login user
GET /api/users?userId=1- Get user by IDGET /api/users?username=john- Get user by usernamePUT /api/users/:id- Update userDELETE /api/users/:id- Delete userPUT /api/users/:id/follow- Follow userPUT /api/users/:id/unfollow- Unfollow userGET /api/users/friends/:userId- Get user's friends
POST /api/posts- Create postGET /api/posts/:id- Get post by IDPUT /api/posts/:id- Update postDELETE /api/posts/:id- Delete postPUT /api/posts/:id/like- Like/unlike postGET /api/posts/timeline/all/:userId- Get timeline feedGET /api/posts/profile/:username- Get user's posts
POST /api/upload- Upload image (profile/post)
id- INTEGER (Primary Key, Auto Increment)username- VARCHAR(20) UNIQUEemail- VARCHAR(50) UNIQUEpassword- VARCHAR (hashed)profilePicture- VARCHARcoverPicture- VARCHARfollowers- INTEGER[] (array)followings- INTEGER[] (array)isAdmin- BOOLEANdesc- VARCHAR(50)createdAt- TIMESTAMPupdatedAt- TIMESTAMP
id- INTEGER (Primary Key, Auto Increment)userId- INTEGER (Foreign Key → users.id)desc- TEXTimg- VARCHARlikes- INTEGER[] (array)dislikes- INTEGER[] (array)comments- JSON[] (array)createdAt- TIMESTAMPupdatedAt- TIMESTAMP
For detailed migration and setup information, see:
POSTGRES_MIGRATION_GUIDE.md- Complete migration guidePOSTGRESQL_SETUP_INSTRUCTIONS.md- Setup and troubleshootingSEQUELIZE_QUICK_REFERENCE.md- Query referenceBEFORE_AFTER_COMPARISON.md- Code comparisonsMIGRATION_COMPLETE.md- Migration summary
Frontend: https://social-med-007.netlify.app
- Password hashing with bcrypt (10 salt rounds)
- JWT token-based authentication
- Helmet security headers
- CORS configuration
- SQL injection prevention via Sequelize
- Foreign key constraints
- Add PostgreSQL database service
- Set environment variables (DATABASE_URL)
- Deploy from GitHub repository
DATABASE_URL=postgresql://user:password@host:5432/dbname
PORT=8800
JWT_SECRET=secure_random_stringThis project was recently migrated from MongoDB to PostgreSQL for:
- ✅ Better performance on complex queries
- ✅ ACID compliance and data integrity
- ✅ Foreign key relationships
- ✅ Industry-standard SQL database
See POSTGRES_MIGRATION_GUIDE.md for details.
Feel free to fork this repository and submit pull requests!
Built with ❤️ using Node.js, Express, and PostgreSQL