Node.js MVC 框架,Paloma 0.2.0 發布
Paloma 是一個類 Angular 的 Node.js MVC 框架。
特性:
-
依賴注入
</li> -
基于 koa@2
</li> </ul>例子:
'use strict';
const Paloma = require('paloma'); const app = new Paloma();
app.controller('indexCtrl', function (ctx, next, indexService) { ctx.body =
Hello, ${indexService.getName()}
; });app.service('indexService', function () { this.getName = function () { return 'paloma'; }; });
app.route({ method: 'GET', path: '/', controller: 'indexCtrl' });
app.listen(3000);</pre>
API
load(dir)
Load all files by require-directory.
Param Type Description </tr> </thead>dir String An absolute path or relative path. </tr> </tbody> </table>route(route)
Register a route.
route
useapp.use
internally, so pay attention to the middleware load order.Param Type Description </tr> </thead>route Object </tr>
route.method String HTTP request method, eg: </tr>GET
,post
.route.path String Request path, see path-to-regexp, eg: </tr>/:name
.route.controller String|Function|[String|Function] Controller functions or names. </tr>route.validate
(optional)Object Validate Object schemas. </tr>route.template
(optional)String View name. If exist, return rendered html. </tr> </tbody> </table>controller(name[, fn])
Register or get a controller. If
fn
missing, return a controller byname
.Param Type Description </tr> </thead>name String Controller name. </tr>fn
(optional)Function Controller handler. </tr>fn->arguments[0]->ctx Object Koa's </tr>ctx
.fn->arguments[1]->next Function Koa's </tr>next
.fn->arguments[2...] Object Instances of services. </tr> </tbody> </table>service(name[, fn])
Register a service constructor or get a service instance. If
fn
missing, return a service instance byname
.Param Type Description </tr> </thead>name String The name of the service. Must be unique to each service instance. </tr>fn Function A constructor function that will be instantiated as a singleton. </tr> </tbody> </table>factory(name, fn)
Register a service factory.
Param Type Description </tr> </thead>name String The name of the service. Must be unique to each service instance. </tr>fn Function A function that should return the service object. Will only be called once; the Service will be a singleton. Gets passed an instance of the container to allow dependency injection when creating the service. </tr> </tbody> </table>provider(name, fn)
Register a service provider.
Param Type Details </tr> </thead>name String The name of the service. Must be unique to each service instance. </tr>fn Function A constructor function that will be instantiated as a singleton. Should expose a function called </tr> </tbody> </table>$get
that will be used as a factory to instantiate the service.constant(name, value)
Register a read only value as a service.
Param Type Details </tr> </thead>name String The name of the constant. Must be unique to each service instance. </tr>value Mixed A value that will be defined as enumerable, but not writable. </tr> </tbody> </table>value(name, value)
Register an arbitrary value as a service.
Param Type Details </tr> </thead>name String The name of the value. Must be unique to each service instance. </tr>value Mixed A value that will be defined as enumerable, readable and writable. </tr> </tbody> </table>decorator([name, ]fn)
Register a decorator function that the provider will use to modify your services at creation time.
Param Type Details </tr> </thead>name
(optional)String The name of the service this decorator will affect. Will run for all services if not passed. </tr>fn Function A function that will accept the service as the first parameter. Should return the service, or a new object to be used as the service. </tr> </tbody> </table>middlewares([name, ]fn)
Register a middleware function. This function will be executed every time the service is accessed. Distinguish with koa's
middleware
.Param Type Details </tr> </thead>name
(optional)String The name of the service for which this middleware will be called. Will run for all services if not passed. </tr>fn Function A function that will accept the service as the first parameter, and a </tr> </tbody> </table>next
function as the second parameter. Should executenext()
to allow other middleware in the stack to execute. Bottle will throw anything passed to thenext
function, i.e.next(new Error('error msg'))
.
來自:https://github.com/palomajs/paloma本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!相關資訊
相關經驗
sesese色