1 | Fast API
|
2 | ========
|
3 |
|
4 | Create RESTful API in seconds.
|
5 |
|
6 | With Fast, you don't have to define routes for each service in your API.<br>
|
7 | Just put your service in your API folder, and the path to the service will be his access point.<br>
|
8 | You can also nest folders, Fast will handle it.
|
9 |
|
10 | Each module should exports 'routes' object which defines the services in this module. <br>
|
11 |
|
12 | In 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 |
|
16 | Inside your service, 'Core' object is available.
|
17 |
|
18 | If 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>
|
19 | Then, call it from other modules like this:<br>
|
20 |
|
21 | var result = Core.api.myservice();
|
22 |
|
23 | You have access to the underscore utility module from:
|
24 |
|
25 | Core.utils
|
26 |
|
27 | You can change the default '/api' path for your API folder.
|
28 | Just add this to the createServer options:
|
29 |
|
30 | apiURIPrefix : "/MY_CUSTOM_PATH"
|
31 |
|
32 | For each service you need to define his params.<br>
|
33 | Only this params with this settings will be valid for this service.<br>
|
34 |
|
35 | By default, Fast will expose for you API documentation in JSON foramat in this path:
|
36 |
|
37 | /YOUR_API_FOLDER/docs
|
38 |
|
39 | You can change it by adding this to the createServer options:
|
40 |
|
41 | apiDocsPath : "MY_CUSTOM_DOCS_PATH"
|
42 | and the path will be:
|
43 |
|
44 | /YOUR_API_FOLDER/MY_CUSTOM_DOCS_PATH
|
45 |
|
46 | To disable the documentation feature, add this to createServer options:
|
47 |
|
48 | exposeDocs : false
|
49 |
|
50 | Fast has built on top of Express so you are more then welcome to fork on github and start hacking.
|
51 |
|
52 |
|
53 | Install
|
54 | ------------
|
55 | npm install fast-api
|
56 |
|
57 | Example:
|
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 |
|
71 | Then, 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 |
|
165 | Go to http://YOUR-APP/api/myservice<br><br>
|
166 | **and........ Voila!**
|
167 |
|
168 | Available 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 |
|
179 | The listen method accept 2 parameters, both are optional:<br>
|
180 |
|
181 | app.listen( port, callback );
|
182 |
|
183 | The default port is 3000.<br>
|
184 | The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
|
185 |
|
186 |
|
187 | Extra Modules
|
188 | -------------
|
189 | You can inject to the Core object one property of you own.<br>
|
190 | For example, if you want to have 'models' module for the DB layer, add this to the options:
|
191 |
|
192 | extraModules : "models"
|
193 |
|
194 | Then create 'models' directory right under your project root.<br>
|
195 | Lets say we have 'message' model. ( meaning 'message.js' file inside this directory )<br>
|
196 | We can call it like this:
|
197 |
|
198 | Core.models.message.setPrivate()
|
199 |
|
200 |
|
201 | Socket.io:
|
202 | ----------
|
203 | To enable socket.io support, add this to the createServer options:
|
204 |
|
205 | enableWebSocket : true,
|
206 | socketKey : "KEY-IN-HANDSHAKE"
|
207 |
|
208 | The socketKey property should hold the property name in the handshake phase of the connection.<br>
|
209 | The value of this propery should hold the identifier for this socket. ( userID for example ).<br>
|
210 | <br>
|
211 | When true, Fast will look for 'subscribers' object that exported from each API end point.<br>
|
212 | This object is the same as the 'routes' object, except the httpMethod property.<br>
|
213 | Also, the paramType for socket params is always 'body'.<br>
|
214 | <br>
|
215 | You can get any socket object using:
|
216 |
|
217 | Core.socket.getSocket( SOCKET-ID )
|
218 |
|
219 |
|
220 |
|
221 | SSL:
|
222 | ----------
|
223 | To 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 |
|