๐ Try it out in under a minute at codespaces.new/kitajs/minimal-example.
๐ด Test out a live deployment at kitajs-minimal-example.up.railway.app.
You can also clone this repository locally:
# clone the repo
git clone https://github.com/kitajs/minimal-example.git app
# Cd into it
cd app
# Install dependencies
npm install
# Build the app
npm run build
# Start the app
npm start
You can find the documentation at kita.js.org. This is a minimal example without any boilerplate or custom configuration, making it the best way to bootstrap your app without unnecessary setup.
Firstly, let's review everything you need to know to get started with KitaJS.
-
Make sure your IDE is using the TypeScript project version. For VSCode, you can check that by opening the command palette and typing
TypeScript: Select TypeScript Version
and selectingUse Workspace Version
. This will add useful debugging information to your IDE. You can test it by writingexport function post(a: 1) {}
intosrc/routes/index.ts
, and you should see an error in your IDE. -
You can start your server by running
npm run build
andnpm start
. -
To add more routes, simply create a new file under
src/routes
. The file name will be the route path; for example,src/routes/users.ts
will be/users
. Afterward, export a function with one of the following names:get
,post
,put
,delete
, orall
. By adding parameters to the newly created method, KitaJS automatically generates route validation, a Swagger schema, and types. -
You must encapsulate your parameters with one of Kita's generic methods:
Body
,BodyProp
,Query
,Path
,Header
, and many others that you can find in the documentation. You can also useFastifyInstance
,FastifyRequest
, andFastifyReply
to access the Fastify instance, request, and reply objects. You can hover over each type to see its documentation and usage examples.
-
In your tsconfig, we added
"plugins": [{ "name": "@kitajs/ts-plugin" }]
to enable your editor to help you with IntelliSense. -
We only need
@kitajs/runtime
andfastify
as production dependencies. You can see the only "configuration" file at src/index.ts.@fastify/swagger
and@fastify/swagger-ui
are only used to generate documentation athttp://localhost:1227/docs
. -
The
build
script callskita build
beforetsc
, which is the correct order because we need to read type information from the source files. -
You can run the
test
script to check if your code compiles correctly.--dry-run
and--noEmit
prevent KitaJS and TypeScript from emitting files, which is useful for testing. -
You can run
npm run dev
to instantly reflect your changes into your server. This is useful for development. -
Start your server by running
node dist/index.js
ornpm start
. -
Done! ๐
After starting your server, you can access http://localhost:1227/documentation
.