Skip to content

A comprehensive full-stack MERN application for efficiently managing and tracking your professional and personal connections, companies, and employment positions.

License

Notifications You must be signed in to change notification settings

Hari31416/connections_backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Connections Tracker - Backend API

A robust Node.js/Express REST API for managing professional and personal connections, companies, and employment positions. Features JWT authentication, MongoDB integration, and comprehensive logging.

⚠️ Experimental Project Disclaimer

This is an experimental project - the result of an experiment to understand whether "vibe coding" can be used to generate end-to-end web applications.

Important Notes:

  • The bulk of this code is AI-generated
  • This code is NOT optimized for production use
  • This project serves as a proof-of-concept and learning exercise
  • Use at your own risk and review thoroughly before any production deployment

πŸš€ Technology Stack

  • Runtime: Node.js
  • Framework: Express.js (v5.1.0)
  • Database: MongoDB with Mongoose ODM (v8.15.0)
  • Authentication: JWT (JSON Web Tokens)
  • Security: bcryptjs for password hashing
  • CORS: Cross-origin resource sharing enabled
  • Environment: dotenv for configuration management

πŸ“‹ Prerequisites

  • Node.js (v16 or higher)
  • MongoDB Atlas account or local MongoDB installation
  • npm or yarn package manager

πŸ› οΈ Installation & Setup

  1. Clone and navigate to backend directory:

    cd backend
  2. Install dependencies:

    npm install
  3. Environment Configuration: Create a .env file in the backend root:

    # Database Configuration
    MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/connections
    
    # JWT Secret (use a strong, random string in production)
    JWT_SECRET=your_super_secure_jwt_secret_key_here
    
    # Server Configuration
    PORT=4000
    NODE_ENV=development
  4. Start the development server:

    npm run dev

    Or for production:

    npm start

πŸ“š API Documentation

Base URL

  • Development: http://localhost:4000/api
  • Production: https://your-backend-domain.com/api

Health Check

  • GET /health - Check server status

Authentication Endpoints

Check System Status

  • GET /api/users/exists
  • Response: { "hasUsers": boolean }

Initialize First Admin User

  • POST /api/initialize
  • Body: { "email": "admin@example.com", "password": "password" }
  • Note: Only works when no users exist in the system

User Login

  • POST /api/login
  • Body: { "email": "user@example.com", "password": "password" }
  • Response: { "token": "jwt_token", "user": {...} }

Register New User (Admin Only)

  • POST /api/register
  • Headers: Authorization: Bearer <admin_jwt_token>
  • Body: { "email": "newuser@example.com", "password": "password" }

Connections Endpoints

Get All Connections

  • GET /api/connections
  • Headers: Authorization: Bearer <jwt_token>

Get Connection by ID

  • GET /api/connections/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Response: Includes connection details and associated positions

Create New Connection

  • POST /api/connections
  • Headers: Authorization: Bearer <jwt_token>
  • Body:
    {
      "name": "John Doe",
      "email": "john@example.com",
      "phone": "+1234567890",
      "linkedinUserId": "johndoe",
      "githubUserId": "johndoe",
      "notes": "Met at tech conference"
    }

Update Connection

  • PUT /api/connections/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Body: Same as create, with updated fields

Delete Connection

  • DELETE /api/connections/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Note: Also deletes all associated positions

Get Connections by Company

  • GET /api/connections/bycompany/:companyId
  • Headers: Authorization: Bearer <jwt_token>

Search Connections

  • GET /api/connections/search/:query
  • Headers: Authorization: Bearer <jwt_token>
  • Description: Search connections across name, email, phone, LinkedIn/GitHub usernames
  • Example: /api/connections/search/john - finds all connections matching "john"
  • Note: Search is case-insensitive and supports partial matches

Companies Endpoints

Get All Companies

  • GET /api/companies
  • Headers: Authorization: Bearer <jwt_token>

Get Company by ID

  • GET /api/companies/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Response: Includes company details and associated positions

Create New Company

  • POST /api/companies
  • Headers: Authorization: Bearer <jwt_token>
  • Body:
    {
      "name": "Tech Corp Inc",
      "industry": "Technology",
      "website": "https://techcorp.com"
    }

Update Company

  • PUT /api/companies/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Body: Same as create, with updated fields

Delete Company

  • DELETE /api/companies/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Note: Also deletes all associated positions

Get Companies by Connection

  • GET /api/companies/byconnection/:connectionId
  • Headers: Authorization: Bearer <jwt_token>

Search Companies

  • GET /api/companies/search/:query
  • Headers: Authorization: Bearer <jwt_token>
  • Description: Search companies across name, industry, and website fields
  • Example: /api/companies/search/tech - finds all companies matching "tech"
  • Note: Search is case-insensitive and supports partial matches

Positions Endpoints

Get All Positions

  • GET /api/positions
  • Headers: Authorization: Bearer <jwt_token>

Get Position by ID

  • GET /api/positions/:id
  • Headers: Authorization: Bearer <jwt_token>

Create New Position

  • POST /api/positions
  • Headers: Authorization: Bearer <jwt_token>
  • Body:
    {
      "connectionId": "connection_object_id",
      "companyId": "company_object_id",
      "title": "Software Engineer",
      "startDate": "2023-01-15",
      "endDate": "2024-01-15",
      "current": false,
      "notes": "Full-stack development role"
    }

Update Position

  • PUT /api/positions/:id
  • Headers: Authorization: Bearer <jwt_token>
  • Body: Same as create, with updated fields

Delete Position

  • DELETE /api/positions/:id
  • Headers: Authorization: Bearer <jwt_token>

Get Positions by Connection

  • GET /api/positions/connection/:connectionId
  • Headers: Authorization: Bearer <jwt_token>

Get Positions by Company

  • GET /api/positions/company/:companyId
  • Headers: Authorization: Bearer <jwt_token>

πŸ—„οΈ Database Models

User Model

{
  _id: ObjectId,
  email: String (required, unique),
  password: String (required, hashed),
  isAdmin: Boolean (default: false),
  createdAt: Date,
  updatedAt: Date
}

Connection Model

{
  _id: ObjectId,
  userId: String (required),
  name: String (required),
  email: String,
  phone: String,
  linkedinUserId: String,
  githubUserId: String,
  notes: String,
  createdAt: Date,
  updatedAt: Date
}

Company Model

{
  _id: ObjectId,
  userId: String (required),
  name: String (required),
  industry: String,
  website: String,
  createdAt: Date,
  updatedAt: Date
}

Position Model

{
  _id: ObjectId,
  userId: String (required),
  connectionId: ObjectId (ref: 'Connection', required),
  companyId: ObjectId (ref: 'Company', required),
  title: String (required),
  startDate: Date,
  endDate: Date,
  current: Boolean (default: false),
  notes: String,
  createdAt: Date,
  updatedAt: Date
}

πŸ” Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: bcryptjs with salt rounds
  • User Isolation: All data is user-scoped via userId
  • Admin Controls: User registration restricted to admin users
  • Request Logging: Comprehensive logging with sensitive data protection
  • CORS Configuration: Cross-origin request handling

πŸ“ Scripts

{
  "start": "node src/index.js",
  "dev": "nodemon src/index.js",
  "test": "echo \"Error: no test specified\" && exit 1"
}

πŸš€ Deployment

Render Deployment (Recommended)

  1. Create Render Web Service:

    • Connect your GitHub repository
    • Set build command: npm install
    • Set start command: npm start
    • Set environment to Node
  2. Environment Variables:

    MONGODB_URI=mongodb+srv://...
    JWT_SECRET=your_production_jwt_secret
    NODE_ENV=production
    PORT=4000
  3. MongoDB Atlas Setup:

    • Create a MongoDB Atlas cluster
    • Add your Render service IP to IP whitelist (or use 0.0.0.0/0 for all IPs)
    • Create a database user with read/write permissions

Alternative Deployment Options

  • Heroku: Compatible with Heroku's Node.js buildpack
  • Railway: Direct GitHub integration
  • Vercel: Serverless functions (requires restructuring)
  • DigitalOcean App Platform: Container-based deployment

πŸ”§ Development

Adding New Routes

  1. Create route file in src/routes/
  2. Import and use in src/index.js
  3. Follow existing authentication middleware pattern

Database Migrations

  • No formal migration system
  • Use MongoDB Compass or Atlas UI for data management
  • Consider implementing seeds for development data

Logging

The application includes comprehensive request logging:

  • All requests are logged with timestamp and IP
  • Sensitive data (passwords, emails, etc.) is hidden in logs
  • Error logging includes stack traces for debugging

πŸ› Troubleshooting

Common Issues

  1. MongoDB Connection Error:

    • Verify MONGODB_URI in .env
    • Check Atlas IP whitelist
    • Ensure database user has correct permissions
  2. JWT Authentication Fails:

    • Verify JWT_SECRET is set
    • Check token format in Authorization header
    • Ensure token hasn't expired
  3. CORS Issues:

    • Configure CORS_ORIGIN for production
    • Verify frontend URL matches CORS settings
  4. Port Already in Use:

    • Change PORT in .env file
    • Kill existing process: lsof -ti:4000 | xargs kill -9

πŸ“„ License

MIT License - see LICENSE file for details

πŸ‘¨β€πŸ’» Author

Harikesh Kushwaha


For frontend documentation, see Frontend README.

About

A comprehensive full-stack MERN application for efficiently managing and tracking your professional and personal connections, companies, and employment positions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published