UNPKG

7.01 kBMarkdownView Raw
1Fast API
2========
3
4Create RESTful API in seconds.
5
6With Fast, you don't have to define routes for each service in your API.<br>
7Just put your service in your API folder, and the path to the service will be his access point.<br>
8You can also nest folders, Fast will handle it.
9
10Each module should exports 'routes' object which defines the services in this module. <br>
11
12In the example below the module 'myservice.js' expose 2 GET services:
13 - http://YOUR-APP/api/myservice/
14 - http://YOUR-APP/api/myservice/:id
15
16Inside your service, 'Core' object is available.
17
18If you want to use one of your services in other module, just expose the service with the name 'service' like the second service in myservice.js file.<br>
19Then, call it from other modules like this:<br>
20
21 var result = Core.api.myservice();
22
23You have access to the underscore utility module from:
24
25 Core.utils
26
27You can change the default '/api' path for your API folder.
28Just add this to the createServer options:
29
30 apiURIPrefix : "/MY_CUSTOM_PATH"
31
32For each service you need to define his params.<br>
33Only this params with this settings will be valid for this service.<br>
34
35By default, Fast will expose for you API documentation in JSON foramat in this path:
36
37 /YOUR_API_FOLDER/docs
38
39You can change it by adding this to the createServer options:
40
41 apiDocsPath : "MY_CUSTOM_DOCS_PATH"
42and the path will be:
43
44 /YOUR_API_FOLDER/MY_CUSTOM_DOCS_PATH
45
46To disable the documentation feature, add this to createServer options:
47
48 exposeDocs : false
49
50Fast has built on top of Express so you are more then welcome to fork on github and start hacking.
51
52
53Install
54------------
55 npm install fast-api
56
57Example:
58----------
59**app.js**
60
61 var Fast = require( 'fast-api' ),
62 Path = require('path' );
63
64 var app = Fast.createServer({
65 apiRoot : Path.join( __dirname, "api" )
66 });
67
68 app.listen( "4000" );
69
70
71Then, in the api folder, you can have this file:
72
73**myservice.js**
74
75 module.exports.routes = {
76 "/" : {
77 summery : "Get list",
78 httpMethod : "get",
79 parameters :[
80 {
81 name : "username",
82 description : "User Name",
83 required : true,
84 dataType : "string",
85 allowMultiple : true,
86 paramType : "query"
87 },
88 {
89 name : "id",
90 description : "User ID",
91 required : false,
92 dataType : "number",
93 allowMultiple : false,
94 paramType : "path"
95 }
96 ],
97
98 service : "service"
99 },
100
101 "/:id" : {
102 summery : "Get list by ID",
103 httpMethod : "get",
104 parameters : [
105 {
106 name : "id",
107 description : "User ID",
108 required : true,
109 dataType : "number",
110 allowMultiple : false,
111 paramType : "path"
112 }
113 ],
114
115 service : "getByID"
116 }
117 };
118
119 module.exports.subscribers = {
120 "set_private" : {
121 summery : "Set Private Message",
122 parameters : [
123 { name : "receiver_id", description : "Receiver ID", required : true, dataType : "string", allowMultiple : false },
124 { name : "sender_id", description : "Sender ID", required : true, dataType : "string", allowMultiple : false },
125 { name : "media_type", description : "Media Type", required : true, dataType : "number", allowMultiple : false },
126 { name : "content", description : "Content", required : true, dataType : "string", allowMultiple : false },
127 { name : "balloon_color", description : "Balloon Color", required : false, dataType : "string", allowMultiple : false },
128 { name : "text_color", description : "Text Color", required : false, dataType : "string", allowMultiple : false }
129 ],
130
131 service : "setPrivate"
132 }
133 };
134
135 module.exports.privileges = {
136 "service" : 0,
137 "getByID" : 1
138 };
139
140 module.exports.getByID = function( req, res ){
141 console.log( req.params.id );
142 };
143
144 module.exports.setPrivate = function( req, res ){
145 req.body.timestamp = Core.date.unix();
146 req.body.sent = 1;
147 req.body.received = 0;
148 req.body.likes = [];
149
150 Core.models.message.setPrivate( req.body ).then( res.success, res.error );
151
152 if( req.isSocket ){
153 var sender = Core.socket.getSocket( req.body.sender_id ),
154 receiver = Core.socket.getSocket( req.body.receiver_id );
155
156 sender.broadcast.to( receiver ).emit( "message", req.body );
157 }
158 };
159
160 module.exports.service = function( req, res ){
161 res.success( "done" );
162 };
163
164
165Go to http://YOUR-APP/api/myservice<br><br>
166**and........ Voila!**
167
168Available options for createServer method and defaults
169--------------
170 {
171 apiURIPrefix : "/api",
172 apiDocsPath : "docs",
173 exposeDocs : true,
174 enableWebSocket : false,
175 webSocketConnectionCallback : false,
176
177 }
178
179The listen method accept 2 parameters, both are optional:<br>
180
181 app.listen( port, callback );
182
183The default port is 3000.<br>
184The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
185
186
187Extra Modules
188-------------
189You can inject to the Core object one property of you own.<br>
190For example, if you want to have 'models' module for the DB layer, add this to the options:
191
192 extraModules : "models"
193
194Then create 'models' directory right under your project root.<br>
195Lets say we have 'message' model. ( meaning 'message.js' file inside this directory )<br>
196We can call it like this:
197
198 Core.models.message.setPrivate()
199
200
201Socket.io:
202----------
203To enable socket.io support, add this to the createServer options:
204
205 enableWebSocket : true,
206 socketKey : "KEY-IN-HANDSHAKE"
207
208The socketKey property should hold the property name in the handshake phase of the connection.<br>
209The value of this propery should hold the identifier for this socket. ( userID for example ).<br>
210<br>
211When true, Fast will look for 'subscribers' object that exported from each API end point.<br>
212This object is the same as the 'routes' object, except the httpMethod property.<br>
213Also, the paramType for socket params is always 'body'.<br>
214<br>
215You can get any socket object using:
216
217 Core.socket.getSocket( SOCKET-ID )
218
219
220
221SSL:
222----------
223To enable SSL, add this to the createServer options:
224
225 useSSL : true
226 useSSL : {
227 key : YOUR_KEY,
228 cert : YOUR_CERT
229 }
230
231
232
233