UNPKG

4.18 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
16For each service you need to define his params.<br>
17Only this params with this settings will be valid for this service.<br>
18
19Inside your service, 'Core' object is available.
20
21If 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>
22Then, call it from other modules like this:<br>
23
24 var result = Core.api.myservice();
25
26You have access to the underscore utility module from:
27
28 Core.utils
29
30
31Fast has built on top of Express so you are more then welcome to fork on github and start hacking.
32
33
34Install
35------------
36 npm install fast-api
37
38Example:
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
52Then, 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
114Go to http://YOUR-APP/api/myservice<br><br>
115**and........ Voila!**
116
117Available 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
128The listen method accept 2 parameters, both are optional:<br>
129
130 app.listen( port, callback );
131
132The default port is 3000.<br>
133The callback gets no params and invoked when Fast finisg the init phase and ready for requests..
134
135
136Socket.io:
137----------
138To enable socket.io support, add this to the createServer options:
139
140 enableWebSocket : true,
141 webSocketConnectionCallback : function( socket ){
142 //your code
143 }
144
145webSocketConnectionCallback is the callback that invoked after new connection.
146You will get the new socket as the first parameter.
147
148SSL:
149----------
150To 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