routes/index ES6 refactoring; + json lambda

This commit is contained in:
tmshv committed 2016-02-24 17:44:11 +03:00
1 parent 33f16a2674
commit 2ad012b27b
1 file changed
+15 -20
+15 -20
View File
@@ -1,21 +1,15 @@
/** import path from 'path';
* Created by tmshv on 01/03/15. import fs from 'mz/fs';
*/ import config from '../config';
var path = require("path"); export function accepts(routes, def) {
var fs = require("mz/fs");
var config = require("../config");
function accepts(routes, def) {
return function *() { return function *() {
var req = this.request; let req = this.request;
var types = Object.keys(routes) let types = Object.keys(routes)
.filter(function (type) { .filter(type => req.accepts(type));
return req.accepts(type);
});
if (types.length) { if (types.length) {
var type = types[0]; let type = types[0];
yield routes[type].apply(this, arguments); yield routes[type].apply(this, arguments);
} else if (def) { } else if (def) {
yield def; yield def;
@@ -25,16 +19,17 @@ function accepts(routes, def) {
}; };
} }
function index(filename) { export function index(filename) {
if (!filename) filename = config['defaultIndex']; if (!filename) filename = config['defaultIndex'];
return function *() { return function *() {
this.type = "text/html"; this.type = 'text/html';
this.body = fs.createReadStream(filename); this.body = fs.createReadStream(filename);
} }
} }
module.exports = { export let json = fn => accepts({
index: index, 'text/html': index(),
accepts: accepts 'text/plain': index(),
}; 'application/json': fn
});