UNPKG

4.72 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.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
133Go to http://YOUR-APP/api/myservice<br><br>
134**and........ Voila!**
135
136Available 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
147The listen method accept 2 parameters, both are optional:<br>
148
149 app.listen( port, callback );
150
151The default port is 3000.<br>
152The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
153
154
155Socket.io:
156----------
157To enable socket.io support, add this to the createServer options:
158
159 enableWebSocket : true,
160 webSocketConnectionCallback : function( socket ){
161 //your code
162 }
163
164webSocketConnectionCallback is the callback that invoked after new connection.
165You will get the new socket as the first parameter.
166
167SSL:
168----------
169To 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