| |
|
Create Fastify Project
Create Fastify Project
Create your first project in Node by use of Fastity with TypeScript in easy way?
Well below are the 6 main step to create a simple Fastify project with TypeScript. Now jump to first step:-
(1)Step 1: Create a New Project
To create a new project first we will create a project directory and to do that we will run the below command in the terminal:-
(2)Step 2: Install Dependencies
In this step once we are ready with directory creation we need to install the dependencies. Here in this step we will install the necessary libraries and the tools:-
(3)Step 3: Initialize TypeScript
Here in this we will need to initialize the typescript and to do that you need to setup TypeScript by creating a tsconfig.json file by using below command:-
npx tsc --init
Now once you run the above command you must need to edit the tsconfig.json to include these key settings:-
(4)Step 4: Initialize Fastify
Now important step is to create a basic fastify server in src/index.ts and below is the command to do that:-
mkdir src
touch src/index.ts
and after above command need to create a file in ths src folder and name is src/index.ts as per given commands:-
(5)Step 5: Set Up Prisma
Here is this step we will initialize Prisma and below command is used to so that:-
npx prisma init
This above commands will creates a prisma folder with a schema.prisma file and a .env file. And below are the 3 step to use prisma to work with database.
(i)Configure Prisma to use MongoDB:-
and here you need to update schema.prisma with below commands:-
(ii)In seconds step for Prisma database setting you need to add the database URL to the .env file:-
(iii)Now last step for Prisma is to generate the Prisma client by the below command:-
npx prisma generate
(6)Step 6: Run the Server
Once you are done with all the above 5 step you need to jump to final step that is run the server and your fastify project. And to run the above project you need to use the ts-node and below is the command:-
npx ts-node src/index.ts
Once you run the above command you need to open the browser and open the http://localhost:3000 url in your browser, and you should see:
Note:-As per the above project you must ensure the below 3 steps:-
(A)You are using TypeScript for better code maintainability.
(B)And here in above project Prisma simplifies database management.
(c)Fastify is famous and lightweight and plugin based design gives you flexibility and performance. | |
|
|
|
|