From 642738790a0af184e79030dd2fdb3dded5150ede Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 7 Jan 2025 14:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4API,=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=B7=B2=E5=BC=83=E7=94=A8API=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 44 ++++++++++++++++++++------------------------ lib/helper.js | 13 +++++++++++-- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 511507e..2f16f5d 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ import 'es.shim' -import { fileURLToPath, parse } from 'node:url' +import { fileURLToPath } from 'node:url' import { dirname, resolve } from 'node:path' import fs from 'iofs' @@ -27,13 +27,8 @@ if (fs.isdir(tmpdir)) { fs.mkdir(tmpdir) -function hideProperty(host, name, value) { - Object.defineProperty(host, name, { - value: value, - writable: true, - enumerable: false, - configurable: true - }) +function urlParse(url) { + return new URL(url, 'http://127.0.0.1') } export default class Request { @@ -46,11 +41,12 @@ export default class Request { #body = null #cookies = Object.create(null) - controller = 'index' method = 'GET' - path = [] - url = '' + controller = 'index' + actions = [] + + pathname = '' host = '127.0.0.1' hostname = '127.0.0.1' protocol = 'http' @@ -74,11 +70,11 @@ export default class Request { // 修正请求的url #init() { - let url = parse(this.#req.url) + let url = urlParse(this.#req.url) .pathname.slice(1) .replace(/[\/]+$/, '') let controller = '' // 将作为主控制器(即apps目录下的应用) - let path = [] + let actions = [] // URL上不允许有非法字符 if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) { @@ -92,26 +88,26 @@ export default class Request { // 修正url中可能出现的"多斜杠" url = url.replace(/[\/]+/g, '/').replace(/^\//, '') - path = url.split('/') - if (!path[0] || path[0] === '') { - path[0] = 'index' + actions = url.split('/') + if (!actions[0] || actions[0] === '') { + actions[0] = 'index' } - if (path[0].includes('.')) { - controller = path[0].slice(0, path[0].indexOf('.')) + if (actions[0].includes('.')) { + controller = actions[0].slice(0, actions[0].indexOf('.')) // 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index if (!controller || controller === '') { controller = 'index' } } else { - controller = path[0] + controller = actions[0] } - path.shift() + actions.shift() this.controller = controller - this.url = url - this.path = path + this.pathname = url + this.actions = actions } /** @@ -225,8 +221,8 @@ export default class Request { get query() { if (!this.#query) { - let data = parse(this.#req.url).query - this.#query = querystring(data) + let { search } = urlParse(this.#req.url) + this.#query = querystring(search.slice(1)) } return this.#query } diff --git a/lib/helper.js b/lib/helper.js index 8cdc155..9f7d8eb 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -7,13 +7,22 @@ import { parse } from 'node:querystring' export function querystring(str) { - let query = parse(str) + let query = {...parse(str)} + for (let k of Object.keys(query)) { let val = query[k] if (k.endsWith('[]')) { let _k = k.slice(0, -2) - query[_k] = val + if (query.hasOwnProperty(_k)) { + let old = query[_k] + if (!Array.isArray(old)) { + old = [old] + } + query[_k] = old.concat(val) + } else { + query[_k] = val + } delete query[k] } else if (k.endsWith(']')) { let idx = k.lastIndexOf('[')