This starter kit provides a solid foundation for building scalable and maintainable Node.js applications using Express, TypeScript, and Prisma ORM. It includes user authentication, role-based access control, and a structure inspired by Laravel's resources and services.
- Express.js with TypeScript
- Prisma ORM for database operations
- JWT-based authentication
- Role-based access control
- Resource-based response formatting
- Service-layer for business logic
- Environment-based configuration
- Basic project structure with clear separation of concerns
Before you begin, ensure you have met the following requirements:
- Node.js (v14.0.0 or later)
- npm (v6.0.0 or later)
- PostgreSQL (v12.0 or later)
To get this project up and running, follow these steps:
-
Clone the repository:
git clone https://github.com/your-username/express-ts-prisma-starter.git cd express-ts-prisma-starter
-
Install dependencies:
npm install
-
Set up your environment variables: Create a
.env
file in the root directory and add the following:DATABASE_URL="postgresql://username:password@localhost:5432/your_database_name?schema=public" JWT_SECRET="your-secret-key" PORT=3000
Replace the placeholder values with your actual database credentials and desired JWT secret.
-
Set up the database:
npx prisma migrate dev
This command will create the database tables based on your Prisma schema.
-
Generate Prisma client:
npx prisma generate
-
Start the development server:
npm run dev
The server should now be running on http://localhost:3000
.
.
├── src
│ ├── config
│ ├── controllers
│ ├── middleware
│ ├── routes
│ ├── services
│ ├── resources
│ ├── prisma
│ ├── utils
│ ├── app.ts
│ └── server.ts
├── .env
├── .gitignore
├── package.json
└── tsconfig.json
config
: Contains configuration files, such as permission constants.controllers
: Handles incoming requests and returns responses.middleware
: Contains reusable middleware functions.routes
: Defines API routes and links them to controllers.services
: Contains the core business logic.resources
: Handles data transformation for API responses.prisma
: Contains the Prisma schema and migrations.utils
: Houses utility functions used across the application.
npm run dev
: Starts the development server with hot-reloading.npm run build
: Compiles TypeScript to JavaScript.npm start
: Starts the production server.npm run prisma:generate
: Generates Prisma client.npm run prisma:migrate
: Runs Prisma migrations.npm run prisma:studio
: Starts Prisma Studio for database management.
When adding new features to this starter kit:
- Create new resources in the
resources
folder for data transformation. - Add business logic to service files in the
services
folder. - Create new controllers in the
controllers
folder, using the services and resources. - Define new routes in the
routes
folder and link them to the controllers. - Update the Prisma schema in
prisma/schema.prisma
if new database models are needed.
- Keep controllers thin by delegating business logic to services.
- Use resources consistently to ensure uniform API responses.
- Write meaningful comments to explain complex logic or important concepts.
- Follow the established patterns and naming conventions when adding new code.
- Use TypeScript's type system to ensure type safety throughout the application.
This project is licensed under the MIT License - see the LICENSE file for details.