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 | For each service you need to define his params.<br>
|
17 | Only this params with this settings will be valid for this service.<br>
|
18 |
|
19 | Inside your service, 'Core' object is available.
|
20 |
|
21 | 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>
|
22 | Then, call it from other modules like this:<br>
|
23 |
|
24 | var result = Core.api.myservice();
|
25 |
|
26 | You have access to the underscore utility module from:
|
27 |
|
28 | Core.utils
|
29 |
|
30 |
|
31 | Fast has built on top of Express so you are more then welcome to fork on github and start hacking.
|
32 |
|
33 |
|
34 | Install
|
35 | ------------
|
36 | npm install fast-api
|
37 |
|
38 | Example:
|
39 | ----------
|
40 | **app.js**
|
41 |
|
42 | var Fast = require( 'fast-api' ),
|
43 | Path = require('path' );
|
44 |
|
45 | var app = Fast.createServer({
|
46 | apiRoot : Path.join( __dirname, "api" )
|
47 | });
|
48 |
|
49 | app.listen( "4000" );
|
50 |
|
51 |
|
52 | Then, in the api folder, you can have this file:
|
53 |
|
54 | **myservice.js**
|
55 |
|
56 | module.exports.routes = {
|
57 | "/" : {
|
58 | summery : "Get list",
|
59 | httpMethod : "get",
|
60 | parameters :[
|
61 | {
|
62 | name : "username",
|
63 | description : "User Name",
|
64 | required : true,
|
65 | dataType : "string",
|
66 | allowMultiple : true,
|
67 | paramType : "query"
|
68 | },
|
69 | {
|
70 | name : "id",
|
71 | description : "User ID",
|
72 | required : false,
|
73 | dataType : "number",
|
74 | allowMultiple : false,
|
75 | paramType : "path"
|
76 | }
|
77 | ],
|
78 |
|
79 | service : "service"
|
80 | },
|
81 |
|
82 | "/:id" : {
|
83 | summery : "Get list by ID",
|
84 | httpMethod : "get",
|
85 | parameters : [
|
86 | {
|
87 | name : "id",
|
88 | description : "User ID",
|
89 | required : true,
|
90 | dataType : "number",
|
91 | allowMultiple : false,
|
92 | paramType : "path"
|
93 | }
|
94 | ],
|
95 |
|
96 | service : "getByID"
|
97 | }
|
98 | };
|
99 |
|
100 | module.exports.privileges = {
|
101 | "service" : 0,
|
102 | "getByID" : 1
|
103 | };
|
104 |
|
105 | module.exports.getByID = function( req, res ){
|
106 | console.log( req.params.id );
|
107 | };
|
108 |
|
109 | module.exports.service = function( req, res ){
|
110 | res.success( "done" );
|
111 | };
|
112 |
|
113 |
|
114 | Go to http://YOUR-APP/api/myservice<br><br>
|
115 | **and........ Voila!**
|
116 |
|
117 | Available options for createServer method and defaults
|
118 | --------------
|
119 | {
|
120 | apiURIPrefix : "/api",
|
121 | apiDocsPath : "docs",
|
122 | exposeDocs : true,
|
123 | enableWebSocket : false,
|
124 | webSocketConnectionCallback : false,
|
125 |
|
126 | }
|
127 |
|
128 | The listen method accept 2 parameters, both are optional:<br>
|
129 |
|
130 | app.listen( port, callback );
|
131 |
|
132 | The default port is 3000.<br>
|
133 | The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
|
134 |
|
135 |
|
136 | Socket.io:
|
137 | ----------
|
138 | To enable socket.io support, add this to the createServer options:
|
139 |
|
140 | enableWebSocket : true,
|
141 | webSocketConnectionCallback : function( socket ){
|
142 | //your code
|
143 | }
|
144 |
|
145 | webSocketConnectionCallback is the callback that invoked after new connection.
|
146 | You will get the new socket as the first parameter.
|
147 |
|
148 | SSL:
|
149 | ----------
|
150 | To enable SSL, add this to the createServer options:
|
151 |
|
152 | useSSL : true
|
153 | useSSL : {
|
154 | key : YOUR_KEY,
|
155 | cert : YOUR_CERT
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|