API Kit Yeoman Generator
This generator provides a convenient way to get up and running APIs using Mongoose and Mystique in no time.
Installation
Api Kit relies on Yeoman to run it's generator code and get things moving quickly. To install Yeoman and API Kit, run:
npm install -g yo generator-api-kit
Generating An API Kit Project
To start, we need an API Kit Project:
yo api-kit
This will ask for an app name and whether the project should be in a new folder. Then, it will npm install all dependencies.
From there, change directories into the new project an run npm start
to start running the API server.
Generating An API Resource
Building an api can be tough, luckily with API Kit, an API resource can be created with a single command:
yo api-kit:resource todo name:String:required done:Boolean completedAt:Date
This will create three files:
// app/models/todo.jsvar Mongoose = ;var Schema = MongooseSchema; var TodoSchema = name: type: String required: true completedAt: type: Boolean done: type: Date; moduleexports = Mongoose;
// app/transformers/todo.jsvar Mystique = ; var TodoTransformer = MystiqueTransformer; Mystique;
// app/http/resources/todos.jsvar express = ;var router = express; router; router; router; router; router; moduleexports = router;
This command also registers the route in app/http/routes.js
:
apiRouter;
Now we can go to http://localhost:3000/api/todos
to see our new API for todos:
If we POST to http://localhost:3000/api/todos
with the body:
Then we should get a response with an id:
And if we make a GET request for http://localhost:3000/api/todos
: