修复无法保存创建订阅的bug

This commit is contained in:
Peng-YM 2020-11-24 22:29:23 +08:00
parent 22c8483f17
commit f9dd77ed41
2 changed files with 25 additions and 8 deletions

View File

@ -27,7 +27,7 @@ function service() {
*/ */
}); });
console.log(welcome); console.log(welcome);
const $app = express({debug: true}); const $app = express();
// Constants // Constants
const SETTINGS_KEY = "settings"; const SETTINGS_KEY = "settings";
const SUBS_KEY = "subs"; const SUBS_KEY = "subs";
@ -3007,15 +3007,19 @@ function express(port = 3000) {
method = method.toUpperCase(); method = method.toUpperCase();
const {path, query} = extractURL(url); const {path, query} = extractURL(url);
// path matching
let handler = null; let handler = null;
let i; let i;
let longestMatchedPattern = 0;
for (i = start; i < handlers.length; i++) { for (i = start; i < handlers.length; i++) {
if (handlers[i].method === "ALL" || method === handlers[i].method) { if (handlers[i].method === "ALL" || method === handlers[i].method) {
const {pattern} = handlers[i]; const {pattern} = handlers[i];
if (patternMatched(pattern, path)) { if (patternMatched(pattern, path)) {
handler = handlers[i]; if (pattern.split("/").length > longestMatchedPattern) {
break; handler = handlers[i];
longestMatchedPattern = pattern.split("/").length;
}
} }
} }
} }
@ -3034,12 +3038,24 @@ function express(port = 3000) {
body, body,
}; };
const res = Response(); const res = Response();
handler.callback(req, res, next).catch((err) => { const cb = handler.callback;
const errFunc = err => {
res.status(500).json({ res.status(500).json({
status: "failed", status: "failed",
message: `Internal Server Error: ${err}`, message: `Internal Server Error: ${err}`,
}); });
}); }
if (cb.constructor.name === 'AsyncFunction') {
cb(req, res, next).catch(errFunc);
} else {
try {
cb(req, res, next);
} catch (err) {
errFunc(err);
}
}
} else { } else {
// no route, return 404 // no route, return 404
const res = Response(); const res = Response();
@ -3216,6 +3232,7 @@ function express(port = 3000) {
} }
} }
} }
/****************************************** Third Party Libraries **********************************************************/ /****************************************** Third Party Libraries **********************************************************/
function heredoc(fn) { function heredoc(fn) {
return fn.toString().split('\n').slice(1, -2).join('\n') + '\n'; return fn.toString().split('\n').slice(1, -2).join('\n') + '\n';

File diff suppressed because one or more lines are too long