Skip to content

Srvshkdm/railway-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Railway Management System API 🚂

A simple API for managing train bookings and schedules. Built with Node.js, Express, and MySQL.

Prerequisites

1. Install MySQL

  • Download and install MySQL from official website
  • Start MySQL service
  • Create database:
CREATE DATABASE railway_db;

2. Set Up Environment

Create a .env file and add these details:

# Database 
DB_HOST=localhost
DB_USER=root         
DB_PASS=yourpassword  
DB_NAME=railway_db    

# API 
PORT=5000
JWT_SECRET=your_jwt_secret
ADMIN_API_KEY=your_admin_api_key


## Project Setup

1. Create project folder:
```bash
mkdir railway-api && cd railway-api
  1. Create these folders:
mkdir config models routes middlewares
(Run these commands separately if dont run together as in whole (eg:- mkdir models))
  1. Create main files:
touch server.js .env README.md(only one .env file will be present)

Your project will look like this:

railway-api/
├── config/        
├── models/        
├── routes/        
├── middlewares/   
└── server.js     # Main file
├── .env
├── package.json
├── read.md
├── package-lock.json(This file need not be created manually occurs after npm install)

Installation

  1. Install Dependencies
npm install express mysql2 sequelize dotenv jsonwebtoken bcryptjs body-parser cors helmet morgan
**OR**
npm intalll (this also works to install all needed dependencies)
  1. Start Server
node server.js

Testing Guide

1)Download Postman in your PC[https://www.postman.com/downloads/]

2)Click New on Top left Corner n Select HTTP to test requests

3)Select Method Type (POST,GET)

4)Paste the Url Provided

5)Change Headers n Body accordingly [Body is in json paste it as raw]

6)Send the Methods

1. Register a User

 POST http://localhost:5000/api/auth/register \
 "Content-Type: application/json" \
   '{
    "name": "John Doe",
    "email": "[email protected]",
    "password": "password",
    "role": "user"
}'

2. Login to Get Token

 POST http://localhost:5000/api/auth/login \
"Content-Type: application/json" \
'{
    "email": "[email protected]",
    "password": "password"
}'

Response:

{
    "token": "your_jwt_token_here"
}

3. Add a Train (Admin Only)

 POST http://localhost:5000/api/trains \
"Content-Type: application/json" \
"admin-api-key: your_admin_api_key" \
-'{
    "name": "Express 101",
    "source": "City A",
    "destination": "City B",
    "totalSeats": 50
}'

4. Book a Seat

POST http://localhost:5000/api/bookings/1
"Content-Type: application/json" 
"Authorization: your_jwt_token_here"

5. View Your Bookings

GET http://localhost:5000/api/bookings 
"Authorization: your_jwt_token_here"

API Endpoints Summary

Users

  • POST /api/auth/register - Create new account
  • POST /api/auth/login - Login to account

Trains

  • GET /api/trains - View available trains
  • POST /api/trains - Add new train (Admin only)

Bookings

  • POST /api/bookings/:trainId - Book a ticket
  • GET /api/bookings - View your bookings

Troubleshooting

  • Make sure MySQL service is running
  • Check if database credentials in .env are correct
  • Ensure all required ports are free (default: 5000)
  • Dont mess up with the same Names

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published