A simple API for managing train bookings and schedules. Built with Node.js, Express, and MySQL.
- Download and install MySQL from official website
- Start MySQL service
- Create database:
CREATE DATABASE railway_db;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- Create these folders:
mkdir config models routes middlewares
(Run these commands separately if dont run together as in whole (eg:- mkdir models))- 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)
- 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)- Start Server
node server.js1)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
POST http://localhost:5000/api/auth/register \
"Content-Type: application/json" \
'{
"name": "John Doe",
"email": "[email protected]",
"password": "password",
"role": "user"
}' POST http://localhost:5000/api/auth/login \
"Content-Type: application/json" \
'{
"email": "[email protected]",
"password": "password"
}'Response:
{
"token": "your_jwt_token_here"
} 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
}'POST http://localhost:5000/api/bookings/1
"Content-Type: application/json"
"Authorization: your_jwt_token_here"GET http://localhost:5000/api/bookings
"Authorization: your_jwt_token_here"POST /api/auth/register- Create new accountPOST /api/auth/login- Login to account
GET /api/trains- View available trainsPOST /api/trains- Add new train (Admin only)
POST /api/bookings/:trainId- Book a ticketGET /api/bookings- View your bookings
- Make sure MySQL service is running
- Check if database credentials in
.envare correct - Ensure all required ports are free (default: 5000)
- Dont mess up with the same Names