A production-ready, type-safe Node.js Express application built with TypeScript, Prisma ORM, PostgreSQL, and JWT authentication.
- 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
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- npm or yarn
- PostgreSQL (v14 or higher)
git clone <repository-url>
cd backend_samplenpm installCopy the example environment file and update it with your configuration:
cp env.example .envEdit .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=7dImportant: Make sure your JWT_SECRET is at least 32 characters long for security.
Generate Prisma Client:
npm run prisma:generateRun database migrations:
npm run prisma:migrateWhen prompted, enter a name for the migration (e.g., "init").
Start the development server with hot reload:
npm run devThe server will start on http://localhost:8000
Build the TypeScript code:
npm run buildStart the production server:
npm startbackend_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
- GET
/api/health- Check server status
-
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" }
-
GET
/api/users/profile- Get current user profile- Requires:
Authorization: Bearer <token>
- Requires:
-
GET
/api/users- Get all users- Requires:
Authorization: Bearer <token>
- Requires:
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>
Open Prisma Studio to view and edit your database:
npm run prisma:studioAfter modifying prisma/schema.prisma:
npm run prisma:migratenpm run prisma:pushnpm run dev- Start development server with hot reloadnpm run build- Compile TypeScript to JavaScriptnpm start- Run production servernpm run prisma:generate- Generate Prisma Clientnpm run prisma:migrate- Run database migrationsnpm run prisma:studio- Open Prisma Studionpm run prisma:push- Push schema changes to databasenpm run lint- Type-check without emitting files
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"- Import the endpoints listed above
- For protected routes, add the JWT token to the Authorization header
- Set the type to "Bearer Token"
- β 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
If you encounter database connection errors:
- Ensure PostgreSQL is running
- Verify your
DATABASE_URLin.env - Check that the database exists
- Ensure the user has proper permissions
If migrations fail:
# Reset the database (WARNING: This will delete all data)
npx prisma migrate reset
# Then run migrations again
npm run prisma:migrateRun type checking:
npm run lint- 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
ISC
fikrirazor (https://github.com/fikrirazor)
endangit (https://github.com/endang-git)
Happy Coding! π