1.2.5 • Published 5 years ago

paloma v1.2.5

Weekly downloads
30
License
MIT
Repository
github
Last release
5 years ago

Paloma

NPM version Build status Dependency Status License Downloads

An angular-like MVC framework, based on:

  • koa@2: Next generation web framework for node.js.
  • bottlejs: A powerful dependency injection micro container.

Installation

$ npm i paloma --save

If you use async function as controller, you may need node v7.6.0+ or babel.

Scaffold

see create-paloma-app.

Example

Common function

const Paloma = require('paloma')
const app = new Paloma()

app.controller('indexCtrl', (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)

/*
$ curl localhost:3000
Hello, Paloma
*/

When a route is matched, its path is available at ctx._matchedRoute.

Async function

const Paloma = require('paloma')
const app = new Paloma()

app.controller('indexCtrl', async (ctx, next, indexService) => {
  ctx.body = await Promise.resolve(`Hello, ${indexService.getName()}`)
})

app.service('indexService', function () {
  this.getName = function () {
    return 'Paloma'
  }
})

app.route({
  method: 'GET',
  path: '/',
  controller: 'indexCtrl'
})

app.listen(3000)

/*
$ curl localhost:3000
Hello, Paloma
*/

or

const Paloma = require('paloma')
const app = new Paloma()

app.route({
  method: 'GET',
  path: '/',
  controller: async (ctx, next, indexService) => {
    ctx.body = await Promise.resolve(`Hello, ${indexService.getName()}`)
  }
})

app.service('indexService', function () {
  this.getName = function () {
    return 'Paloma'
  }
})

app.listen(3000)

routerName

const Paloma = require('paloma')
const app = new Paloma()

app.route({
  method: 'GET',
  path: '/',
  routerName: 'getHome',
  controller: (ctx, next) => {
    ctx.body = `routerName: ${ctx.state.routerName}`
  }
})

app.listen(3000)

/*
$ curl localhost:3000
routerName: getHome
*/

Validator

const Paloma = require('paloma')
const app = new Paloma()

app.controller('indexCtrl', (ctx, next) => {
  ctx.body = `Hello, ${ctx.query.name}`
})

app.route({
  method: 'GET',
  path: '/',
  controller: 'indexCtrl',
  validate: {
    query: {
      name: { type: 'string', enum: ['tom', 'xp'], required: true }
    }
  }
})

app.listen(3000)
/*
$ curl localhost:3000
($.query.name: undefined) ✖ (required: true)
$ curl localhost:3000?name=tom
Hello, tom
$ curl localhost:3000?name=nswbmw
($.query.name: "nswbmw") ✖ (enum: tom,xp)
*/

More validators usage see another-json-schema.

Array controllers

const bodyParser = require('koa-bodyparser')
const Paloma = require('paloma')
const app = new Paloma()

app.controller('indexCtrl', (ctx, next) => {
  ctx.body = ctx.request.body
})

app.route({
  method: 'POST',
  path: '/',
  controller: [bodyParser(), 'indexCtrl']
})

app.listen(3000)

More examples see test and paloma-examples.

API

load(dir)

Load all files by require-directory.

ParamTypeDescription
dirStringAn absolute path or relative path.

route(route)

Register a route. route use app.use internally, so pay attention to the middleware load order.

ParamTypeDescription
routeObject
route.methodStringHTTP request method, eg: GET, post.
route.pathStringRequest path, see path-to-regexp, eg: /:name.
route.controllerString|Function|String|FunctionController functions or names.
route.validate(optional)ObjectValidate Object schemas.

controller(name, fn)

Register or get a controller. If fn missing, return a controller by name.

ParamTypeDescription
nameStringController name.
fn(optional)FunctionController handler.
fn->arguments0->ctxObjectKoa's ctx.
fn->arguments1->nextFunctionKoa's next.
fn->arguments2...ObjectInstances of services.

service(name, fn)

Register a service constructor or get a service instance. If fn missing, return a service instance by name.

ParamTypeDescription
nameStringThe name of the service. Must be unique to each service instance.
fnFunctionA constructor function that will be instantiated as a singleton.

factory(name, fn)

Register a service factory.

ParamTypeDescription
nameStringThe name of the service. Must be unique to each service instance.
fnFunctionA 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.

provider(name, fn)

Register a service provider.

ParamTypeDetails
nameStringThe name of the service. Must be unique to each service instance.
fnFunctionA constructor function that will be instantiated as a singleton. Should expose a function called $get that will be used as a factory to instantiate the service.

constant(name, value)

Register a read only value as a service.

ParamTypeDetails
nameStringThe name of the constant. Must be unique to each service instance.
valueMixedA value that will be defined as enumerable, but not writable.

value(name, value)

Register an arbitrary value as a service.

ParamTypeDetails
nameStringThe name of the value. Must be unique to each service instance.
valueMixedA value that will be defined as enumerable, readable and writable.

decorator(name, fn)

Register a decorator function that the provider will use to modify your services at creation time.

ParamTypeDetails
name(optional)StringThe name of the service this decorator will affect. Will run for all services if not passed.
fnFunctionA function that will accept the service as the first parameter. Should return the service, or a new object to be used as the service.

middlewares(name, fn)

Register a middleware function. This function will be executed every time the service is accessed. Distinguish with koa's middleware.

ParamTypeDetails
name(optional)StringThe name of the service for which this middleware will be called. Will run for all services if not passed.
fnFunctionA function that will accept the service as the first parameter, and a next function as the second parameter. Should execute next() to allow other middleware in the stack to execute. Bottle will throw anything passed to the next function, i.e. next(new Error('error msg')).

use(fn) &

see koa.

License

MIT

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.0

7 years ago

0.5.0

7 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.0

8 years ago