Create node rest api using kiwi!

The idea of this post is to create a basic api rest with typescript in a few steeps using kiwi and kiwi-cli.

This two tools are  in a npm package that you can install. Also you can contribute to the growth of the packages.

If you want to learn more about kiwi server framework you can go here

1. Install the npm packages.

> npm install kiwi-server --save
> npm install kiwi-server-cli -g

Now we already have all packages that we need to develop a node api with typescript.

2. We are going to create a server so we have to use the following command.

> kc init


This command will generate the basic server, the environment and the configuration that we need to begin.
It creates a server.ts  server, tsconfig.json witch is the basic configuration to translate the typescript code and finally the environment folder with two environments that we can select at building time.

3. Now is time to generate our controllers.

> kc controller pathname/controllername


This command will generate you controller on pathname with controllername. If the pathname doesn’t exist it will create it for you.
Now we have a sample controller to start but we have to tell the server that this controller is going to be exposed. So we are going to add this controller on the server configuration.

4. Finally we have to build our code to start the server.

kc build


> kc build -e <env>

If we don’t specify an environment it will use environment.ts by default.
If we specify an environment it will use environment.<env>.ts file.


If you see the image you can see that we are going to have a dist folder with an environment structure inside for each environment.

5. Finally you only have to start the server.

> node dist/default/server.ts

6. If you want to create middlewares after or before you have to exectue the follow commands

> kc middleware -t <before|after> path/middlewarename

If you like the post please star our repository here

Related posts