Mention API is a robust backend service built with Express.js and TypeScript, providing functionality for social media interactions including posts, user management, authentication, and real-time communications.
- Node.js with TypeScript
- Express.js for REST API
- MongoDB with Mongoose for data storage
- Socket.IO for real-time features
- JWT for authentication
- Node.js (v14 or higher)
- MongoDB instance
- npm or yarn package manager
- Clone the repository
- Install dependencies:
npm install
- Create a
.env
file in the root directory with the following variables:
MONGODB_URI=your_mongodb_connection_string
ACCESS_TOKEN_SECRET=your_jwt_access_token_secret
REFRESH_TOKEN_SECRET=your_jwt_refresh_token_secret
PORT=3000
Development mode:
npm run dev
Production mode:
npm run build
npm start
# Build the Docker image
docker build -t mention-api .
# Run the container
docker run -p 3000:3000 -e MONGODB_URI=your_mongodb_uri mention-api
- Configure
vercel.json
:
{
"version": 2,
"builds": [{
"src": "dist/server.js",
"use": "@vercel/node"
}],
"routes": [{
"src": "/(.*)",
"dest": "dist/server.js"
}]
}
- Deploy using Vercel CLI:
vercel --prod
sequenceDiagram
participant Client
participant API
participant Database
Client->>API: POST /auth/signup
API->>Database: Create User
Database-->>API: User Created
API-->>Client: Access Token
Client->>API: POST /auth/login
API->>Database: Verify Credentials
Database-->>API: User Found
API-->>Client: Access + Refresh Tokens
- Creates a new user account
- Body:
{ username: string, email: string, password: string }
- Returns:
{
"user": {
"id": "user_id",
"username": "john_doe",
"email": "[email protected]",
"createdAt": "2023-01-01T00:00:00Z"
},
"accessToken": "eyJhbGciOiJIUzI1NiIs..."
}
- Authenticates existing user
- Body:
{ email: string, password: string }
- Returns:
{
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs..."
}
- Creates a new post
- Authentication: Bearer token required
- Body:
{
"text": "Hello world!",
"media": ["image1.jpg"],
"location": {
"longitude": -73.935242,
"latitude": 40.730610
}
}
- Returns:
{
"id": "post_id",
"text": "Hello world!",
"media": ["image1.jpg"],
"location": {
"type": "Point",
"coordinates": [-73.935242, 40.730610]
},
"createdAt": "2023-01-01T00:00:00Z",
"user": {
"id": "user_id",
"username": "john_doe"
}
}
- Retrieves posts for exploration
- Query params:
limit
(default: 20),offset
(default: 0) - Returns:
{
"posts": [
{
"id": "post_id",
"text": "Post content",
"user": {
"id": "user_id",
"username": "john_doe"
},
"createdAt": "2023-01-01T00:00:00Z"
}
],
"total": 100,
"hasMore": true
}
- Retrieves user profile
- Authentication: Bearer token required
- Returns:
{
"id": "user_id",
"username": "john_doe",
"bio": "Hello, I'm John!",
"followersCount": 1000,
"followingCount": 500,
"postsCount": 100,
"isFollowing": true
}
erDiagram
User ||--o{ Post : creates
User ||--o{ Bookmark : has
User ||--o{ Follow : follows
Post ||--o{ Like : receives
Post ||--o{ Comment : has
User {
string id PK
string username
string email
string password
string[] bookmarks
datetime createdAt
}
Post {
string id PK
string userId FK
string text
string[] media
object location
datetime createdAt
}
- Implement Redis caching for:
- User profiles (TTL: 1 hour)
- Popular posts (TTL: 15 minutes)
- Trending hashtags (TTL: 5 minutes)
// User Collection Indexes
db.users.createIndex({ "username": 1 }, { unique: true })
db.users.createIndex({ "email": 1 }, { unique: true })
// Post Collection Indexes
db.posts.createIndex({ "userId": 1, "createdAt": -1 })
db.posts.createIndex({ "location": "2dsphere" })
GET /health
Response: {
"status": "healthy",
"version": "1.0.0",
"uptime": 1000,
"mongoStatus": "connected"
}
- Use Winston for structured logging
- Log levels: error, warn, info, debug
- Include request ID in all logs
- Connection Timeouts
Error: MongoTimeoutError
Solution: Check MongoDB connection string and network connectivity
- Authentication Failures
Error: JsonWebTokenError
Solution: Verify token expiration and secret keys
- Rate Limit Exceeded
Error: 429 Too Many Requests
Solution: Implement exponential backoff in client
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
AGPL License