A complete backend API for a booking system application built with Node.js, Express, Prisma, and TypeScript. This project includes features like secure authentication, role-based authorization, property and booking management, and advanced data validation.
- Secure Authentication: User registration and login with JWT (JSON Web Tokens).
- Role-Based Authorization: Middleware to protect routes for
USERandADMINroles. - Full CRUD for Properties: Admin-only endpoints to create, read, update, and delete properties.
- Complex Booking Logic: Endpoints for users to create, edit, and delete bookings with ownership checks and date conflict detection.
- Advanced Validation: Schema-based input validation using
zod. - Database Seeding: A script to automatically create an admin user.
- Centralized Error Handling: A single middleware to handle all application errors gracefully.
- Pagination: Scalable endpoints for fetching lists of data.
- Backend: Node.js, Express.js
- Language: TypeScript
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT, bcryptjs
- Validation: Zod
- Infrastructure: Docker
Follow these instructions to get a copy of the project up and running on your local machine.
- Node.js (v18 or later)
- PostgreSQL
- Docker (optional)
-
Clone the repository
git clone [https://github.com/Ghost-web-ops/booking-api-nodejs.git](https://github.com/Ghost-web-ops/booking-api-nodejs.git) cd booking-api-nodejs -
Install dependencies
npm install
-
Set up environment variables
-
Create a
.envfile in the root directory. -
Add the following variables (replace with your own values):
DATABASE_URL="postgresql://YOUR_USER:YOUR_PASSWORD@localhost:5432/YOUR_DB_NAME" JWT_SECRET="YOUR_SUPER_SECRET_KEY"
-
-
Run database migrations
npx prisma migrate dev
-
Seed the database (This will create the first admin user)
npx prisma db seed
-
Start the development server
npm run dev
The server will be available at
http://localhost:3000.
A brief overview of the main API endpoints:
POST /api/auth/register: Register a new user.POST /api/auth/login: Log in a user and get a JWT.GET /api/properties: Get a list of all properties.POST /api/bookings: Create a new booking (Authentication required).
For full details, please refer to the API documentation or the code.