1 | Module:
|
2 | ifnode([options: Object])
|
3 |
|
4 | Options:
|
5 | alias: String "Nice" name of application [optional] By default: equal to id
|
6 | project_folder: String Full path to application root project folder [optional] By default: equal to process.argv[1]
|
7 | environment: String Name of application config (folder ${project_folder}/config) [optional]
|
8 |
|
9 | projectFolder Alias of option .project_folder
|
10 | env: String Alias of option .environment
|
11 |
|
12 | Example:
|
13 | var app = ifnode({
|
14 | project_folder: __dirname,
|
15 | alias: 'application',
|
16 | env: 'dev'
|
17 | });
|
18 |
|
19 |
|
20 | Result of ifnode() (application instance):
|
21 |
|
22 | Constants:
|
23 | .config Application config
|
24 | .server Instance of http.createServer
|
25 | .listener Instance of express()
|
26 | .components Hash of application components
|
27 | .models Hash of application models
|
28 | .controllers Hash of application controllers
|
29 | .id Application id (is auto-generated by GUID function)
|
30 | .alias "Nice" name of application By default: equal to id
|
31 | .project_folder Full path to application root project folder By default: equal to process.argv[1]
|
32 | .backend_folder Full path to application backend folder (folder with all components) By default: ${project_folder}/protected
|
33 |
|
34 | .projectFolder Alias of .project_folder
|
35 | .backendFolder Alias of .backend_folder
|
36 |
|
37 | Methods:
|
38 | .load() Load of application components (models, components and controllers)
|
39 | .run([callback: Function]) Run application server. Invoke .load() method if not invoked early
|
40 | .down([callback: Function]) Stop application server
|
41 | .extension(name: String) Get application`s extension by name
|
42 | .register(name: String|module: Module|list_of_names: Array) Attach modules for ifnode application. Module must be added to /node_modules folder or extensions (own application independent modules)
|
43 | Example: ifnode-auth, ifnode-mongoose, etc
|
44 |
|
45 | Usage:
|
46 | var ifnode = require('ifnode'),
|
47 | app = ifnode();
|
48 |
|
49 | app.register('ifnode-auth');
|
50 |
|
51 | .ext() Alias of .extension()
|
52 | .require() Alias of .extension()
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | Config:
|
58 | Description json object with all application options. Can contain any options (same of options parse by ifnode)
|
59 | Path to config`s folder ${project_folder}/config
|
60 |
|
61 | Example:
|
62 |
|
63 | module.exports = {
|
64 | site: {
|
65 | port: 1010
|
66 | }
|
67 | };
|
68 |
|
69 | ifnode options:
|
70 | site: Description of site url
|
71 | local: Local url (use for start server)
|
72 | host: [optional] By default: localhost
|
73 | port: [optional] By default: 8080
|
74 | global: Global url ("nice" domain of site)
|
75 | host: [optional] By default: localhost
|
76 | port: [optional] By default: null
|
77 | ssl: SSL support (check https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener)
|
78 | key: Full or relative (at project_folder) path to key.pem (just path, not need read as file)
|
79 | cert: Full or relative (at project_folder) path to cert.pem (just path, not need read as file)
|
80 | pfx: Full or relative (at project_folder) path to pfx.pem (just path, not need read as file)
|
81 | Note: ifnode generate config option .site with options:
|
82 | .origin Equal to: http(s)://${host}[:${port}] (if set site.ssl - https, other - http)
|
83 | .url(pathname: String) Method for generate link
|
84 |
|
85 | Example:
|
86 | // config/dev.js
|
87 | site:
|
88 | local:
|
89 | port: 1010
|
90 | global:
|
91 | host: 'nicedomainname.io'
|
92 | ssl:
|
93 | key: 'ssl/key.pem',
|
94 | cert: 'ssl/cert.pem'
|
95 |
|
96 | // app.js
|
97 | var app = ifnode({ env: 'dev' }),
|
98 | site = app.config.site;
|
99 |
|
100 | site.local.origin // https://localhost:1010
|
101 | site.local.url('/u/1') // https://localhost:1010/u/1
|
102 | site.global.origin // https://nicedomainname.io
|
103 | site.global.url('u/1') // https://nicedomainname.io/u/1
|
104 |
|
105 | application: Application (and express()) settings
|
106 | environment: Name of environment. By default: name of config file (if config by file) or "local"
|
107 | express: Hash of express application settings (list: http://expressjs.com/4x/api.html#app.set)
|
108 | view engine: By default: jade
|
109 | views: By default: ${backend_folder}/views
|
110 | x-powered-by By default: false (sorry, express :) )
|
111 | env: By default: name of config file (if config by file) or "local"
|
112 | folders:
|
113 | extensions: Folder of extensions. By default: ${backend_folder}/extensions
|
114 | components: Folder of components. By default: ${backend_folder}/components
|
115 | views: Folder of views. By default: ${backend_folder}/views
|
116 | controllers: Folder of controllers. By default: ${backend_folder}/controllers
|
117 | models: Folder of models. By default: ${backend_folder}/models
|
118 |
|
119 |
|
120 | middleware: External middleware modules (examples: connect-multiparty, cookie-parser etc)
|
121 | ifnode middlewares:
|
122 | body: Build on body-parser module
|
123 | '${parser}': options ${parser} - name of parser, options - hash of parser options. See here: https://github.com/expressjs/body-parser
|
124 | statics: Build on serve-statics module. Three variants of initializing of static files
|
125 | path: String Options see here: https://github.com/expressjs/serve-static
|
126 | variants: Object
|
127 | path1: options,
|
128 | path2: options,
|
129 | ...
|
130 | variant: Array
|
131 | path,
|
132 | path1,
|
133 | { path2: options },
|
134 | ...
|
135 |
|
136 | db: Initialize options of databases (models schemas)
|
137 | name_of_db: Name of database connect
|
138 | schema: name of schema Schema name (for choose of schema)
|
139 | config: schema config Config of database connection
|
140 |
|
141 | components: Initialize options for components of ifnode
|
142 | name_of_component: options
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | Controller:
|
149 | app.Controller([options: Object])
|
150 |
|
151 | Controller can be extended by external plugins (example: ifnode-auth, ifnode-permissions etc)
|
152 |
|
153 | Default options:
|
154 | root: String Root path to request [optional] By default: /path/to/controller/
|
155 | name: String Name of controller [optional] By default: path/to/controller
|
156 | router: Object express.Router options. [optional] By default: {}
|
157 | See here: http://expressjs.com/4x/api.html#router
|
158 | map: Object Mapping system for describe controller [optional] By default: null
|
159 |
|
160 | ajax: null|Boolean AJAX settings of controller`s actions [optional] By default: undefined
|
161 | before: String|Function|Array Handlers which invoke before route handlers [optional] By default: []
|
162 |
|
163 | Note: controller`s filename !.js or ~.js are special. Those filenames not participate in forming name and root options.
|
164 | !.js: always initialize first in folder. Next loaded controllers into folders, and next files
|
165 | ~.js: always initialize last in folder
|
166 |
|
167 | Example:
|
168 | /protected/controllers/api/v1/!.js
|
169 | var controller = app.Controller();
|
170 | controller.name // api/v1
|
171 | controller.root // /api/v1/
|
172 |
|
173 | /protected/controllers/api/v1/user.js
|
174 | var controller = app.Controller({ root: '/user' );
|
175 | controller.name // api/v1/user
|
176 | controller.root // /user
|
177 |
|
178 | Static methods (use for creating controller`s plugins):
|
179 | .process_config Add handler for controller`s config
|
180 | .populate Add route middleware for populate arguments own options
|
181 | .middleware Add route middleware for process request
|
182 |
|
183 | Example: see ifnode-auth plugin
|
184 |
|
185 | Constants:
|
186 | .id Application id (is auto-generated by GUID function)
|
187 | .root Controller`s root path
|
188 | .name Controller`s name
|
189 | .router express.Router() instance
|
190 |
|
191 | Instance methods:
|
192 | .param(param: String, handler: Function) Analog of express`s app.param. See here: http://expressjs.com/4x/api.html#app.param
|
193 | .method(method: String [, path: String] [, options: Object], callback: Function [, callback: Function...]) Method for creating controller`s route.
|
194 | method Name of method. Current available: get, post, put, patch, delete
|
195 | path Next part of url. [optional] By default: /
|
196 | See here: http://expressjs.com/4x/api.html#router.METHOD
|
197 | Note: instead of express`s variant of set, cannot be regular expression
|
198 | options Special options for route [optional] By default: equal to controller`s options
|
199 | callback Handler(s) of request
|
200 |
|
201 | "Syntax sugar" for .method():
|
202 | .get()
|
203 | .post()
|
204 | .put()
|
205 | .patch()
|
206 | .delete()
|
207 | .del()
|
208 |
|
209 | .end() End of controller (if request cannot have handler invoke response.not_found() method. By default go to next controller to find request handler)
|
210 | .use([path: String,] callback: Function [, callback: Function...]) See here: http://expressjs.com/4x/api.html#app.use
|
211 | .error(handler: Function) Invoke .error() handler when any request handler`s next() method of controller get instance of Error class. By default try find parent error handler, and if not find throw error in node.js
|
212 |
|
213 | Note: All handlers request and response methods populated by same options.
|
214 | request:
|
215 | .data "Syntax sugar". Return request.body, or request.query or null
|
216 |
|
217 | response:
|
218 | .ok([data: String|Object]) Set status 200 and return data
|
219 | .fail([data: String|Object]) Set status 400 and return data or text "Bad Request"
|
220 | .error([data: String|Object]) Set status 500 and return data or text "Server Internal Error"
|
221 | .unauthorized([data: String|Object]) Set status 401 and return data
|
222 | .forbidden([data: String|Object]) Set status 403 and return data
|
223 | .not_found([data: String|Object]) Set status 404 and return data
|
224 |
|
225 | .bad_request() Alias of .fail()
|
226 | .badRequest() Alias of .fail()
|
227 | .err() Alias of .error()
|
228 | .notFound() Alias of .not_found()
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | Model:
|
235 | app.Model(model_options: Object [, schema_options: Object])
|
236 |
|
237 | All model`s schemas must be added by plugins
|
238 |
|
239 | model_options: Options to create model. Specified by model`s schema. Recommended options:
|
240 | .name: String Name of model
|
241 | .table: String Name of table (for SQL databases: MySQL, PostgreSQL, etc)
|
242 | .collection: String Name of collection (for NoSQL databases: mongodb, redis, etc)
|
243 | .columns: Object Describe table or collection columns (example: mongoose columns)
|
244 |
|
245 | schema_options: Options for build model`s schema. Specified by model`s schema.
|
246 | .db: String Name of database (from config.db) By default: first in config.db or "virtual" type
|
247 | .alias: String|Array Aliases of model (in app.models)
|
248 |
|
249 |
|
250 | Create own Schema for creating models:
|
251 | .schema Name of schema
|
252 | .driver(db_config: Object) Static method for initialize database driver. Add to Schema.fn returned driver [optional]
|
253 |
|
254 | .fn Alias of Schema.prototype
|
255 | .fn.initialize(model_config: Object) Create model instance by schema [optional]
|
256 | .fn.compile() Return final model instance (this model can access by app.models)
|
257 |
|
258 | .fn.init() Alias of .fn.initialize()
|
259 |
|
260 | Example:
|
261 | own-schema.js
|
262 | exports.schema = function(app, Schema) {
|
263 | Schema.schema = 'name-of-schema';
|
264 | Schema.driver = function(db_config) {
|
265 | ...
|
266 | return driver;
|
267 | };
|
268 |
|
269 | Schema.fn.initialize = function(model_config) { ... };
|
270 | Schema.fn.compile = function() {
|
271 | ...
|
272 | return ifnode_model;
|
273 | };
|
274 | };
|
275 |
|
276 |
|
277 | knex.js
|
278 | var _ = require('lodash'),
|
279 | knex = require('knex');
|
280 |
|
281 | exports.schema = function(app, Schema) {
|
282 | Schema.schema = 'knex';
|
283 | Schema.driver = function(db_config) {
|
284 | return knex(db_config);
|
285 | };
|
286 |
|
287 | Schema.fn.initialize = function(model_config) {
|
288 | this.table = model_config.table;
|
289 | this.driver = this._driver;
|
290 | this.all = function() {
|
291 | return this.driver(this.table);
|
292 | };
|
293 | };
|
294 | Schema.fn.compile = function() {
|
295 | return this;
|
296 | };
|
297 | };
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | Components:
|
304 | app.Component([options: Object])
|
305 |
|
306 | Special module for application. Initialize after models and can use them
|
307 |
|
308 | Default options:
|
309 | name: String Name of controller [optional] By default: id
|
310 |
|
311 | Methods:
|
312 | .initialize(config: Object) Function to initialize component (get config from ifnode config)
|