Next generation web framework for building APIs
Kite focus on writing APIs more efficiently with latest Javascript features
Current version 0.5.10:
- This version requires TypeScript >= 3.7 to run !
- Performance improved
- Function kite.route() replace config.route
- HttpRouter becoming the default router, configuration is not required, default route to <workdir>/controllers/*.controller.js
Introduction
Kite is a lightweight web application framework, it's designed for efficient coding of APIs. Here are differences between other hot frameworks ( ExpressJS, KoaJS):
- Kite is written in TypeScript with latest ECMAScript features: Decorators, Async function / await
- Kite applications should be written in TypeScript too (recommended), it makes the source more simple, clear and maintainable
- NO REST ROUTE in Kite (not yet), no route definition required in Kite, just place your API in a structured directory, the framework will map to your code, one api for one source file
- Built-in data type convertor, Kite converts input data to your declared types, it can convert input strings to JavaScript "Number", "Boolean", "Date", MongoDB "ObjectId" or any other types
- MongoDB's losing document schema is here! By declaring a model class, Kite filters & converts input data before database operation, post JSON objects with nested objects / complex documents is pretty simple! And this feature is also works for SQL databases
- Dependance injection supported, Kite manages dependent modules for you, each application service is created & managed in a single instance for reuse and data sharing
- Enjoy coding, in development mode Kite detects file changes and reload relative modules within 2 seconds, so you don't need to stop & start your application server when source code is changed, this feature makes you focus on coding and testing. Unlike third-party node modules (nodemon / pm2) restart the whole application server, Kite only reloads the changed files and their relative modules.
Use Kite cli tools to quickly start your application!
npm i kite-tools -g
kite --help
Quick start
Create a kite project:
kite -p --yes first-kite-app
Explanation for the upper command:
- kite : Kite CLI tools command name
- -p option : create and initialize a Kite project
- --yes option : create without questioning, use default configuration
- first-kite-app option: application name, here we use "first-kite-app"
Then Kite CLI tools will create a folder first-kite-app, initialize the project folder with some files and directories, install NodeJS dependencies as well.
Making Kite fly
If the above steps finished without error (generally network problems), now you can enter the project folder, and start Kite application server:
cd first-kite-app
npm start
After a few seconds waiting for TypeScript compilation / NodeJS running, you'll see a message like:
2018-3-23 10:13:24 [ KITE ] Flying! server listening at 127.0.0.1:4000
which means Kite application server is successfully started, then open a browser window and visit 127.0.0.1:4000(or localhost:4000), you will get a message like:
{"error":{"code":1002,"msg":"Resource not found"}}
don't worry about this error message, it's just means the request resource / was not found on server, your application just works great.
First API
Using kite-tools, you can easily create Kite APIs. Open a new terminal (keep previous terminal running, which servicing your application), under project folder type this command:
kite -a greet
Kite CLI tools will generate a file named greet.controller.ts under src/controllers/ folder, it looks like this:
import { Controller, Entry, KiteError, Inject } from 'kite-framework';
@Controller()
export class GreetController {
@Entry()
async exec() {
throw new KiteError(1000, 'this api is not implemented');
}
}
then run command tsc to compile this TypeScript source:
tsc
Now you can try this API by visiting http://localhost:4000/greet, here is the response:
{"error":{"code":1000,"msg":"this api is not implemented"}}
Hello world! (plain text output)
Simply returns a string from APIs will cause the framework outputs content in text/plain type. API source greet.controller.ts example:
import { Controller, Entry, KiteError, Inject } from 'kite-framework';
@Controller() export class GreetController {
@Entry()
async exec() {
return "hello world!";
}
}
Save and run tsc to compile ( boring with typing tsc everytime? open a new terminal and try tsc -w, see TypeScript compiler watch mode ).
Now refresh the page( http://localhost:4000/greet), response:
hello world!
Hello world! (json output)
KiteJS is a framework focusing on web APIs, an API is generally output a JSON formatted string, and in JavaScript/NodeJs, why not JSON?
Returns an object from APIs will cause the framework outputs content in application/json type. API source greet.controller.ts example:
import { Controller, Entry, KiteError, Inject } from 'kite-framework';
@Controller()
export class GreetController {
@Entry()
async exec() {
return { msg: "hello world!" };
}
}
Compile the source (if not running tsc -w) and refresh the page, response like:
{"msg":"hello world!"}
Accepting client parameters
Declared arguments in Kite entry point function ( exec() as of yet) is mapped to client parameters by KiteJS framework at controller loading time.
Here is an example (create a new controller welcome.controller.ts by kite-tools kite -a welcome):
import { Controller, Entry, KiteError, Inject } from 'kite-framework';
@Controller()
export class WelcomeController {
@Entry()
async exec(name: string) {
return { msg: `Hello, ${name}!` };
}
}
Compile the source (if not running tsc -w), and visit http://localhost:4000/welcome?name=Kite, response like:
{"msg":"Hello, Kite!"}