0.5.1 • Published 5 years ago

app-controller v0.5.1

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

app-controller

Part of app helpers project.

Simplify express route handlers.
Aggregate request params into single object (params, body, query).
Takes care of response sending.
Works with promises.
You only need to return value from route handler.

Installation

npm install app-controller

Usage

var app = require('express')();
var controller = require('app-controller');
var bodyParser = require('body-parser');
var Promise = require('bluebird');

app.use(bodyParser());

// Response body will be json array -> [1, 2, 3]
app.get('/numbers', controller(function(params, req) {
	return [1,2,3];
}));

// Same with promise -> [1, 2, 3]
app.get('/numbers', controller(function(params, req) {
	return Promise.resolve([1,2,3]);
}));

// Use params
// GET /numbers?pivot=2 -> [3]
app.get('/numbers', controller(function(params, req) {
	return [1, 2, 3].filter(function(n) {
		return n > params.pivot;
	});
}));

// POST /login
// Request body:
// username: johny
// password: qwerty
app.post('/login', controller(function(params, req) {
	return someAsyncAuthChecker(params.username, params.password).then(function() {
		return { success: true };
	});
}));

// And so on..

License

MIT

0.5.1

5 years ago

0.5.0

7 years ago

0.4.0

8 years ago

0.3.0

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago