Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boo1ean/app-controller
App http response helper
https://github.com/boo1ean/app-controller
Last synced: 30 days ago
JSON representation
App http response helper
- Host: GitHub
- URL: https://github.com/boo1ean/app-controller
- Owner: boo1ean
- Created: 2014-10-28T13:37:27.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-02-15T19:08:04.000Z (over 5 years ago)
- Last Synced: 2024-10-05T04:46:04.139Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 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
```javascript
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