π 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 startYou 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 Versionand 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 buildandnpm 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.tswill 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, andFastifyReplyto 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/runtimeandfastifyas production dependencies. You can see the only "configuration" file at src/index.ts.@fastify/swaggerand@fastify/swagger-uiare only used to generate documentation athttp://localhost:1227/docs. -
The
buildscript callskita buildbeforetsc, which is the correct order because we need to read type information from the source files. -
You can run the
testscript to check if your code compiles correctly.--dry-runand--noEmitprevent KitaJS and TypeScript from emitting files, which is useful for testing. -
You can run
npm run devto instantly reflect your changes into your server. This is useful for development. -
Start your server by running
node dist/index.jsornpm start. -
Done! π
After starting your server, you can access http://localhost:1227/documentation.
