Table of contents
In this blog post, I’ll guide you through building a RESTful API using Express.js and TypeScript, along with Prisma ORM for database operations and JWT for user authentication. This API includes CRUD operations for posts and user authentication functionalities, ensuring secure access through JWT-based authentication.
🚀 Features
This project has the following features:
👤 User Registration and Login: Secure user authentication.
🔐 JWT-based Authentication: Protect routes by verifying JWT tokens.
📝 CRUD Operations for Posts: Create, Read, Update, and Delete posts.
🐘 PostgreSQL Database with Prisma ORM: Database operations are handled by Prisma ORM.
📚 Swagger API Documentation: Automatically generated API documentation using Swagger.
🚨 Comprehensive Error Handling and Logging: Built-in error handling and logging functionality.
🌐 CORS Support: Handle cross-origin resource sharing.
📋 Prerequisites
Before you begin, ensure you have the following installed on your system:
Node.js (v14 or later)
npm or yarn
PostgreSQL database (We’re using Neon Postgres)
🛠 Installation
1. Clone the repository:
git clone https://github.com/azharhussain/Express-TypeScript-API-with-Prisma-and-Authentication.git
cd Express-TypeScript-API-with-Prisma-and-Authentication
2. Install the dependencies:
npm install
3. Set up your environment variables:
Create a .env
file in the root directory and add the following:
DATABASE_URL="your_postgresql_connection_string"
JWT_SECRET="your_jwt_secret"
PORT=1000
4. Set up the database:
Run the Prisma migration command to initialize the database schema:
npx prisma migrate dev --name init
🚀 Usage
To start the development server, run:
npm run dev
The server will start at http://localhost:1000 (or any port you’ve specified in the .env
file).
📚 API Documentation
Once the server is running, you can access the Swagger API documentation by navigating to: http://localhost:1000/api-docs
🛠 API Endpoints
Authentication Routes:
POST /api/auth/register: Register a new user.
POST /api/auth/login: Login an existing user.
Post Routes:
GET /api/posts: Get all posts.
GET /api/posts/:id: Get a specific post by ID.
POST /api/posts: Create a new post (requires authentication).
PUT /api/posts/:id: Update an existing post (requires authentication).
DELETE /api/posts/:id: Delete a post (requires authentication).
🔍 Testing
To test the API endpoints, you can use tools like Postman or Insomnia. A Postman collection is also provided in the repository to make testing easier.
Steps to Test:
Import the Postman collection into your Postman workspace.
After logging in, update the
authToken
variable in Postman with a valid JWT token.Test the available API endpoints.
📝 Logging
Logging is handled with pino
. The logs are written to two files:
error.log: Captures error logs.
combined.log: Logs all activities.
In development mode, logs are also printed to the console for easier debugging.
👥 Contributing
Contributions are welcome! If you’d like to improve the project, follow these steps:
Fork the repository.
Create a new branch:
git checkout -b feature/your-feature-name
Make your changes.
Commit the changes:
git commit -am 'Add some feature'
Push to the branch:
git push origin feature/your-feature-name
Create a new Pull Request.
📜 License
This project is licensed under the MIT License. Feel free to use and modify it as per your requirements.
📞 Contact
If you have any questions or feedback, feel free to reach out to me:
Website: Azhar Hussain
Email: azharfastian@gmail.com
By following this guide, you’ll have a robust RESTful API with Express, TypeScript, Prisma, and JWT authentication ready to go. Happy coding!