A RESTful API server for a social media platform, built with Node.js, Express, and PostgreSQL. It supports user authentication, posting, following, commenting, liking, and search functionalities.
- User registration, login, and JWT authentication
- Password reset via email
- Create, read, update, and delete posts
- Like and comment on posts
- Follow/unfollow users
- User profiles with followers/following lists
- Search for users and posts
- Trending topics
- Rate limiting, input validation, and security best practices
- Node.js, Express
- PostgreSQL
- JWT for authentication
- bcrypt for password hashing
- Helmet & CORS for security
- Nodemailer for email
- Node.js (v16+ recommended)
- PostgreSQL
- Clone the repository:
git clone <repo-url> cd social-media-server
- Install dependencies:
npm install
- Set up your environment variables (see below).
- Set up the database:
- Create a PostgreSQL database.
- Run the schema in
database/schema.sqlto create tables.
Create a .env file in the root directory with the following:
PORT=5000
NODE_ENV=development
DATABASE_URL=postgres://<user>:<password>@<host>:<port>/<db>
JWT_SECRET=your_jwt_secret
JWT_EXPIRE=30d
EMAIL_USER=your_email@example.com
EMAIL_PASS=your_email_password
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
See database/schema.sql for full details. Main tables:
users: User accountsposts: Posts by userscomments: Comments on postslikes: Likes on postsfollowers: Follower/following relationships
POST /api/auth/register— Register a new userPOST /api/auth/login— LoginGET /api/auth/me— Get current userPOST /api/auth/forgot-password— Request password resetPUT /api/auth/reset-password/:token— Reset passwordPUT /api/auth/change-password— Change password
POST /api/posts— Create postGET /api/posts— Get posts (feed)GET /api/posts/feed— Get feed posts (paginated)GET /api/posts/explore— Get explore posts (paginated)GET /api/posts/:id— Get single postPUT /api/posts/:id— Update postDELETE /api/posts/:id— Delete postPUT /api/posts/:id/like— Like/unlike postPOST /api/posts/:id/comments— Add commentDELETE /api/posts/:id/comments/:comment_id— Delete comment
GET /api/profiles/:id— Get user profilePUT /api/profiles/:id/follow— Follow/unfollow userGET /api/profiles/:id/posts— Get user's postsGET /api/profiles/:id/followers— Get user's followersGET /api/profiles/:id/following— Get user's followingGET /api/profiles/search— Search usersGET /api/profiles/suggestions— Suggested users
GET /api/search/users— Search usersGET /api/search/posts— Search postsGET /api/search/trending— Trending topics
See api-tests.http for ready-to-run API request examples (compatible with VS Code REST Client extension).
npm startThe server will run on http://localhost:5000 by default.
ISC