This project is a Railway Management System API similar to IRCTC, built using Node.js & Express.js with PostgreSQL as the database. It allows users to check train availability, book seats, and manage train data while ensuring security and concurrency.
- Backend: Node.js, Express.js
- Database: PostgreSQL
- Authentication: JWT (JSON Web Token)
- Concurrency: Database Transactions(ACID Properties)
- Middleware: Express Middleware
- Security: API Key Protection for Admin Routes
git clone https://github.com/imsachin49/trainSync.gitcd into the project directory
cd trainSyncnpm installCreate a .env file in the root directory and add the following environment variables:
JWT_SECRET=
ADMIN_API_KEY=
PORT=
DATABASE_URL=
// PostgreSQL Connection URL: postgres://username:password@host:port/database (OR use one from NeonDB)npm start- User Authentication: Users must log in to get a JWT token.
- Admin Protection: Admin routes require an API key in the headers.
- Booking Security: Users need a valid JWT token to book or view bookings.
- GET
/health: Check if the server is running.
- Description: Register a new user.
- Body (Required):
{ "username": "john_doe", // Required "password": "securepassword123", // Required "email": "john.doe@example.com" // Required }
- Description: Authenticate user and return a JWT token.
- Body (Required):
{ "email": "john.doe@example.com", // Required "password": "securepassword123" // Required }
- Description: Add a new train.
- Body (Required):
{ "name": "Express 101", // Required "source": "New York", // Required "destination": "Boston", // Required "totalSeats": 100 // Required (Must be > 0) } - Headers:
x-api-key: Admin API Key (Required)
- Description: Fetch all trains.
- Description: Update total seats for a train.
- Params:
trainId(Required): ID of the train to update.
- Body (Required):
{ "totalSeats": 120 // Required (Must be > 0) } - Headers:
x-api-key: Admin API Key (Required)
- Description: Check seat availability between
sourceanddestination. - Query Params (Required):
source: Source station (e.g.,New York).destination: Destination station (e.g.,Boston).
- Example Request:
GET /api/v1/trains/availability?source=New%20York&destination=Boston
- Description: Book a seat on a train.
- Body (Required):
{ "trainId": 1 // Required (ID of the train to book) }
- Description: Fetch booking details by
bookingId. - Params:
bookingId(Required): ID of the booking to fetch.
- Description: Cancel a booking by
bookingId. - Params:
bookingId(Required): ID of the booking to cancel.