SIMPLE NEST.JS TASK API WITH SWAGGER AND JWT AUTH.
- Node.js(version 18 upward)
- Docker
- npm
git clone https://github.com/LexxLuey/basic-nest
cd basic-nest
Make sure Docker is installed and running on your system. Then, execute the following command to start the PostgreSQL container using Docker Compose:
docker-compose up -d
This command will pull the PostgreSQL image and create a container named postgres
with the specified configuration in docker-compose.yml
.
Install the project dependencies using npm:
npm install
If you haven't installed the Prisma CLI globally, you can install it using npm:
npm install -g prisma
Follow the prompts and choose the PostgreSQL database option. Provide the required database connection details.
Define your database schema in schema.prisma
file according to your project requirements. This has already been done for Task and User models.
To run the NestJS application in development mode, follow these steps:
- Start PostgreSQL Container: Ensure Docker is installed and running on your system. Run the following command to start the PostgreSQL container using Docker Compose (If you have done this already, SKIP):
docker-compose up -d
- Set Environment Variables: Create a
.env
file in the root directory of the project if it doesn't already exist. Define the following environment variables:
DATABASE_URL=postgresql://<username>:<password>@localhost:5432/<database_name>
JWT_SECRET=<your_jwt_secret_key>
Replace <username>
, <password>
, and <database_name>
with your PostgreSQL database credentials and database name. Choose a secure value for JWT_SECRET
.
- Install Dependencies: Install the project dependencies using npm:
npm install
- Run Prisma Migration: Generate and apply Prisma migrations by running the following commands:
Generate the Prisma Client by running the following command:
prisma generate
Then apply migrations:
npx prisma migrate dev --name "init"
This command will create the necessary tables in your PostgreSQL database based on your Prisma schema.
- Start the NestJS Application: Once the PostgreSQL container is running, the Prisma migrations are applied, and the dependencies are installed, you can start the NestJS application:
npm run start:dev
This command will start the NestJS application in development mode. By default, the application will listen on port 5000.
- Access the Application: You can now access the NestJS application by navigating to
http://localhost:5000/api
in your web browser.