tojava
Version:
这是一个server服务支持ftl
79 lines (67 loc) • 2.11 kB
JavaScript
var R = require('ramda');
var fs = require('fs');
var path = require('path');
var util = require('../util/util.js');
var jspserver = require('./fmk-jsp');
/**
* 根据配置文件规则加载数据
* @param {} route
* @param {} config
* @param {} req
* @param {} res
* @returns {}
*/
module.exports = R.curry(function(route, config, req, res, next) {
var head = R.head(route);
if (!head) {
res.send("请配置数据文件模板文件");
return;
}
var sendJson = function(json) {
function jsonfn() {
res.set("Content-Type", "application/json");
res.send(json);
}
function jsonpfn() {
res.set("Content-Type", "application/json");
res.send(R.reduce(R.concat, req.query.callback, ["(", json, ")"]));
}
R.ifElse(R.isNil, jsonfn, jsonpfn)(R.path(['query', 'callback'], req));
};
var sendHtml = function(html) {
res.set("Content-Type", "text/html");
res.send(html);
};
var readFile = util.readFile;
var abspath = function(path1) {
return R.ifElse(R.test(/json$/),
R.always(path.join(config.jettry.root,
path1)),
R.identity)(path1);
};
var parsepath = function(path1) {
req.cwd = path.join(__dirname, '..');
req.config = config;
return util.formatstr(path1, req);
};
function callnext() {
next();
}
function send(str) {
res.send(str);
}
function jspReq(head) {
jspserver.req(parsepath(head),
R.map(parsepath, R.concat(config.project.request, R.tail(route))),
send);
}
R.cond([
[R.test(/json$/), R.pipe(parsepath, abspath, R.ifElse(fs.existsSync,
R.pipe(readFile, sendJson),
callnext))],
[R.test(/html$/), R.pipe(parsepath, abspath, R.ifElse(fs.existsSync,
R.pipe(readFile, sendHtml),
callnext))],
[R.T, jspReq]
])(head);
});