2019-02-28 07:00:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-21 05:27:25 +00:00
|
|
|
function eggSend(obj) {
|
|
|
|
/*jslint validthis: true*/
|
|
|
|
var me = this;
|
|
|
|
if (!me.getHeader('content-type')) {
|
|
|
|
me.setHeader('Content-Type', 'application/json');
|
|
|
|
}
|
|
|
|
me.end(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
|
2019-02-28 07:00:23 +00:00
|
|
|
module.exports = function eggspress() {
|
|
|
|
//var patternsMap = {};
|
|
|
|
var allPatterns = [];
|
|
|
|
var app = function (req, res) {
|
|
|
|
var patterns = allPatterns.slice(0).reverse();
|
2019-03-07 08:38:21 +00:00
|
|
|
function next(err) {
|
|
|
|
if (err) {
|
|
|
|
req.end(err.message);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-28 07:00:23 +00:00
|
|
|
var todo = patterns.pop();
|
|
|
|
if (!todo) {
|
|
|
|
console.log('[eggspress] Did not match any patterns', req.url);
|
|
|
|
require('finalhandler')(req, res)();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// '', GET, POST, DELETE
|
|
|
|
if (todo[2] && req.method.toLowerCase() !== todo[2]) {
|
|
|
|
//console.log("[eggspress] HTTP method doesn't match", req.url);
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:23:53 +00:00
|
|
|
var urlstr = (req.url.replace(/\/$/, '') + '/');
|
|
|
|
if (!urlstr.match(todo[0])) {
|
2019-02-28 07:00:23 +00:00
|
|
|
//console.log("[eggspress] pattern doesn't match", todo[0], req.url);
|
|
|
|
next();
|
|
|
|
return;
|
2019-03-28 09:23:53 +00:00
|
|
|
} else if ('string' === typeof todo[0] && 0 !== urlstr.match(todo[0]).index) {
|
2019-02-28 07:00:23 +00:00
|
|
|
//console.log("[eggspress] string pattern is not the start", todo[0], req.url);
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:05:37 +00:00
|
|
|
function fail(e) {
|
2019-02-28 07:00:23 +00:00
|
|
|
console.error("[eggspress] error", todo[2], todo[0], req.url);
|
|
|
|
console.error(e);
|
|
|
|
// TODO make a nice error message
|
|
|
|
res.end(e.message);
|
2019-03-09 12:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
console.log("[eggspress] matched pattern", todo[0], req.url);
|
|
|
|
var p = todo[1](req, res, next);
|
|
|
|
if (p && p.catch) {
|
|
|
|
p.catch(fail);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
fail(e);
|
2019-02-28 07:00:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 05:27:25 +00:00
|
|
|
|
|
|
|
res.send = eggSend;
|
|
|
|
|
2019-02-28 07:00:23 +00:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
app.use = function (pattern, fn) {
|
|
|
|
return app._use('', pattern, fn);
|
|
|
|
};
|
2019-03-28 09:23:53 +00:00
|
|
|
[ 'HEAD', 'GET', 'POST', 'DELETE' ].forEach(function (method) {
|
2019-02-28 07:00:23 +00:00
|
|
|
app[method.toLowerCase()] = function (pattern, fn) {
|
|
|
|
return app._use(method, pattern, fn);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post = function (pattern, fn) {
|
|
|
|
return app._use('POST', pattern, fn);
|
|
|
|
};
|
|
|
|
app._use = function (method, pattern, fn) {
|
|
|
|
// always end in a slash, for now
|
|
|
|
if ('string' === typeof pattern) {
|
|
|
|
pattern = pattern.replace(/\/$/, '') + '/';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (!patternsMap[pattern]) {
|
|
|
|
patternsMap[pattern] = [];
|
|
|
|
}
|
|
|
|
patternsMap[pattern].push(fn);
|
|
|
|
patterns = Object.keys(patternsMap).sort(function (a, b) {
|
|
|
|
return b.length - a.length;
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
allPatterns.push([pattern, fn, method.toLowerCase()]);
|
|
|
|
return app;
|
|
|
|
};
|
|
|
|
|
|
|
|
return app;
|
|
|
|
};
|