Skip to content

fikrirazor/ticketon-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

61 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ticketon Backend - Type-Safe Node.js Express Application

A production-ready, type-safe Node.js Express application built with TypeScript, Prisma ORM, PostgreSQL, and JWT authentication.

πŸš€ Features

  • TypeScript - Full type safety across the application
  • Express.js - Fast, unopinionated web framework
  • Prisma ORM - Modern database toolkit with type-safe queries
  • PostgreSQL - Robust relational database
  • JWT Authentication - Secure token-based authentication
  • Yup Validation - Schema validation for environment variables and request data
  • Performance Optimized - Database indexing and Gzip response compression
  • Error Handling - Centralized error handling middleware
  • Security - Helmet and CORS protection
  • Hot Reload - Development server with automatic restart

πŸ“‹ Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (v18 or higher)
  • npm or yarn
  • PostgreSQL (v14 or higher)

πŸ› οΈ Installation

1. Clone the repository

git clone <repository-url>
cd backend_sample

2. Install dependencies

npm install

3. Set up environment variables

Copy the example environment file and update it with your configuration:

cp env.example .env

Edit .env and configure the following variables:

NODE_ENV=development
PORT=8000
DATABASE_URL=postgresql://username:password@localhost:5432/database_name?schema=public
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
JWT_EXPIRES_IN=7d

Important: Make sure your JWT_SECRET is at least 32 characters long for security.

4. Set up the database

Generate Prisma Client:

npm run prisma:generate

Run database migrations:

npm run prisma:migrate

When prompted, enter a name for the migration (e.g., "init").

πŸƒ Running the Application

Development Mode

Start the development server with hot reload:

npm run dev

The server will start on http://localhost:8000

Production Mode

Build the TypeScript code:

npm run build

Start the production server:

npm start

πŸ“ Project Structure

backend_sample/
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma          # Prisma schema definition (with indexes)
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ database.ts        # Prisma client configuration
β”‚   β”‚   └── env.ts             # Environment variable validation
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts # Authentication logic
β”‚   β”‚   β”œβ”€β”€ event.controller.ts # Event management & filtering
β”‚   β”‚   β”œβ”€β”€ organizer.controller.ts # Organizer dashboard & statistics
β”‚   β”‚   β”œβ”€β”€ review.controller.ts # Review & rating system
β”‚   β”‚   └── transaction.controller.ts # Transaction & payment logic
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ auth.middleware.ts # JWT verification & role protection
β”‚   β”‚   β”œβ”€β”€ error.middleware.ts # Centralized error handling
β”‚   β”‚   β”œβ”€β”€ upload.middleware.ts # Cloudinary file upload
β”‚   β”‚   └── validation.middleware.ts # Request validation using Yup
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth.routes.ts, event.routes.ts, etc.
β”‚   β”‚   └── index.ts           # Route aggregation
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ transaction.cron.ts # Automated transaction expiry
β”‚   β”‚   β”œβ”€β”€ jwt.util.ts, password.util.ts, etc.
β”‚   β”‚   └── logger.ts          # Unified logging system
β”‚   β”œβ”€β”€ app.ts                 # Express app (Security, Compression, Routes)
β”‚   └── index.ts               # Server entry point & graceful shutdown
└── README.md                  # This file

πŸ”Œ API Endpoints

Health Check

  • GET /api/health - Check server status

Authentication

  • POST /api/auth/signup - Register a new user

    {
      "email": "[email protected]",
      "password": "SecurePass123"
    }
  • POST /api/auth/signin - Sign in a user

    {
      "email": "[email protected]",
      "password": "SecurePass123"
    }

Users (Protected Routes)

  • GET /api/users/profile - Get current user profile

    • Requires: Authorization: Bearer <token>
  • GET /api/users - Get all users

    • Requires: Authorization: Bearer <token>

πŸ” Authentication

This application uses JWT (JSON Web Tokens) for authentication. After signing in or signing up, you'll receive a token that must be included in the Authorization header for protected routes:

Authorization: Bearer <your-jwt-token>

πŸ—„οΈ Database Management

Prisma Studio

Open Prisma Studio to view and edit your database:

npm run prisma:studio

Create a new migration

After modifying prisma/schema.prisma:

npm run prisma:migrate

Push schema changes without migration

npm run prisma:push

πŸ“ Available Scripts

  • npm run dev - Start development server with hot reload
  • npm run build - Compile TypeScript to JavaScript
  • npm start - Run production server
  • npm run prisma:generate - Generate Prisma Client
  • npm run prisma:migrate - Run database migrations
  • npm run prisma:studio - Open Prisma Studio
  • npm run prisma:push - Push schema changes to database
  • npm run lint - Type-check without emitting files

πŸ§ͺ Testing the API

Using cURL

Sign Up:

curl -X POST http://localhost:8000/api/auth/signup \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"SecurePass123"}'

Sign In:

curl -X POST http://localhost:8000/api/auth/signin \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"SecurePass123"}'

Get Profile (Protected):

curl -X GET http://localhost:8000/api/users/profile \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Using Postman or Thunder Client

  1. Import the endpoints listed above
  2. For protected routes, add the JWT token to the Authorization header
  3. Set the type to "Bearer Token"

πŸ”’ Security Best Practices

  • βœ… Environment variables validated with Yup
  • βœ… Passwords hashed with bcrypt (10 salt rounds)
  • βœ… JWT tokens with configurable expiration
  • βœ… Helmet.js for security headers
  • βœ… CORS protection
  • βœ… Input validation with Yup schemas
  • βœ… Type-safe database queries with Prisma
  • βœ… Error handling without exposing sensitive information

πŸ› Troubleshooting

Database Connection Issues

If you encounter database connection errors:

  1. Ensure PostgreSQL is running
  2. Verify your DATABASE_URL in .env
  3. Check that the database exists
  4. Ensure the user has proper permissions

Migration Issues

If migrations fail:

# Reset the database (WARNING: This will delete all data)
npx prisma migrate reset

# Then run migrations again
npm run prisma:migrate

TypeScript Errors

Run type checking:

npm run lint

πŸ“š Tech Stack

  • Runtime: Node.js
  • Language: TypeScript
  • Framework: Express.js
  • ORM: Prisma
  • Database: PostgreSQL
  • Authentication: JWT (jsonwebtoken)
  • Validation: Yup
  • Password Hashing: bcrypt
  • Security: Helmet, CORS
  • Dev Tools: ts-node-dev, nodemon

πŸ“„ License

ISC

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

fikrirazor (https://github.com/fikrirazor)

endangit (https://github.com/endang-git)


Happy Coding! πŸš€

About

TicketOn Backend dibangun menggunakan Express, Prisma dan Yup Validation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors