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.privileges = {
|
120 | "service" : 0,
|
121 | "getByID" : 1
|
122 | };
|
123 |
|
124 | module.exports.getByID = function( req, res ){
|
125 | console.log( req.params.id );
|
126 | };
|
127 |
|
128 | module.exports.service = function( req, res ){
|
129 | res.success( "done" );
|
130 | };
|
131 |
|
132 |
|
133 | Go to http://YOUR-APP/api/myservice<br><br>
|
134 | **and........ Voila!**
|
135 |
|
136 | Available options for createServer method and defaults
|
137 | --------------
|
138 | {
|
139 | apiURIPrefix : "/api",
|
140 | apiDocsPath : "docs",
|
141 | exposeDocs : true,
|
142 | enableWebSocket : false,
|
143 | webSocketConnectionCallback : false,
|
144 |
|
145 | }
|
146 |
|
147 | The listen method accept 2 parameters, both are optional:<br>
|
148 |
|
149 | app.listen( port, callback );
|
150 |
|
151 | The default port is 3000.<br>
|
152 | The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
|
153 |
|
154 |
|
155 | Socket.io:
|
156 | ----------
|
157 | To enable socket.io support, add this to the createServer options:
|
158 |
|
159 | enableWebSocket : true,
|
160 | webSocketConnectionCallback : function( socket ){
|
161 | //your code
|
162 | }
|
163 |
|
164 | webSocketConnectionCallback is the callback that invoked after new connection.
|
165 | You will get the new socket as the first parameter.
|
166 |
|
167 | SSL:
|
168 | ----------
|
169 | To enable SSL, add this to the createServer options:
|
170 |
|
171 | useSSL : true
|
172 | useSSL : {
|
173 | key : YOUR_KEY,
|
174 | cert : YOUR_CERT
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|