node-web-mvc
Version:
node spring mvc
41 lines (40 loc) • 1.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const querystring_1 = __importDefault(require("querystring"));
const ForwardEndlessLoopError_1 = __importDefault(require("../../errors/ForwardEndlessLoopError"));
class ApplicationDispatcher {
constructor(filterAdapter, url) {
this.url = String(url);
const [path, query] = this.url.split('?');
this.path = path;
this.query = query;
this.filterAdapter = filterAdapter;
}
forward(request, response) {
const servletContext = request.servletContext;
const isLoop = !!servletContext.forwardStacks.find((f) => f === this.url);
if (isLoop) {
// 如果循环跳转
throw new ForwardEndlessLoopError_1.default(servletContext.forwardStacks);
}
// 添加跳转栈,用于检测是否循环跳转
servletContext.forwardStacks.push(this.url);
return this.processRequest(request, response);
}
processRequest(request, response) {
const origin = new URL(request.path, 'http://localhost');
const originPath = origin.pathname;
const isRelative = this.path[0] !== '/';
const idx = originPath.lastIndexOf('/');
const pathName = isRelative ? originPath.slice(0, idx) + '/' + this.path : this.path;
// 覆写参数
request.path = pathName;
request.query = Object.assign(Object.assign({}, (request.query || {})), (querystring_1.default.parse((this.query || '').slice(1))));
request.pathVariables = {};
return this.filterAdapter.doFilter(request, response);
}
}
exports.default = ApplicationDispatcher;