调整API, 修改已弃用API的使用
parent
f433720268
commit
642738790a
44
index.js
44
index.js
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import 'es.shim'
|
import 'es.shim'
|
||||||
|
|
||||||
import { fileURLToPath, parse } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import { dirname, resolve } from 'node:path'
|
import { dirname, resolve } from 'node:path'
|
||||||
import fs from 'iofs'
|
import fs from 'iofs'
|
||||||
|
|
||||||
|
@ -27,13 +27,8 @@ if (fs.isdir(tmpdir)) {
|
||||||
|
|
||||||
fs.mkdir(tmpdir)
|
fs.mkdir(tmpdir)
|
||||||
|
|
||||||
function hideProperty(host, name, value) {
|
function urlParse(url) {
|
||||||
Object.defineProperty(host, name, {
|
return new URL(url, 'http://127.0.0.1')
|
||||||
value: value,
|
|
||||||
writable: true,
|
|
||||||
enumerable: false,
|
|
||||||
configurable: true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Request {
|
export default class Request {
|
||||||
|
@ -46,11 +41,12 @@ export default class Request {
|
||||||
#body = null
|
#body = null
|
||||||
#cookies = Object.create(null)
|
#cookies = Object.create(null)
|
||||||
|
|
||||||
controller = 'index'
|
|
||||||
method = 'GET'
|
method = 'GET'
|
||||||
path = []
|
|
||||||
|
|
||||||
url = ''
|
controller = 'index'
|
||||||
|
actions = []
|
||||||
|
|
||||||
|
pathname = ''
|
||||||
host = '127.0.0.1'
|
host = '127.0.0.1'
|
||||||
hostname = '127.0.0.1'
|
hostname = '127.0.0.1'
|
||||||
protocol = 'http'
|
protocol = 'http'
|
||||||
|
@ -74,11 +70,11 @@ export default class Request {
|
||||||
|
|
||||||
// 修正请求的url
|
// 修正请求的url
|
||||||
#init() {
|
#init() {
|
||||||
let url = parse(this.#req.url)
|
let url = urlParse(this.#req.url)
|
||||||
.pathname.slice(1)
|
.pathname.slice(1)
|
||||||
.replace(/[\/]+$/, '')
|
.replace(/[\/]+$/, '')
|
||||||
let controller = '' // 将作为主控制器(即apps目录下的应用)
|
let controller = '' // 将作为主控制器(即apps目录下的应用)
|
||||||
let path = []
|
let actions = []
|
||||||
|
|
||||||
// URL上不允许有非法字符
|
// URL上不允许有非法字符
|
||||||
if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) {
|
if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) {
|
||||||
|
@ -92,26 +88,26 @@ export default class Request {
|
||||||
// 修正url中可能出现的"多斜杠"
|
// 修正url中可能出现的"多斜杠"
|
||||||
url = url.replace(/[\/]+/g, '/').replace(/^\//, '')
|
url = url.replace(/[\/]+/g, '/').replace(/^\//, '')
|
||||||
|
|
||||||
path = url.split('/')
|
actions = url.split('/')
|
||||||
if (!path[0] || path[0] === '') {
|
if (!actions[0] || actions[0] === '') {
|
||||||
path[0] = 'index'
|
actions[0] = 'index'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0].includes('.')) {
|
if (actions[0].includes('.')) {
|
||||||
controller = path[0].slice(0, path[0].indexOf('.'))
|
controller = actions[0].slice(0, actions[0].indexOf('.'))
|
||||||
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
|
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
|
||||||
if (!controller || controller === '') {
|
if (!controller || controller === '') {
|
||||||
controller = 'index'
|
controller = 'index'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
controller = path[0]
|
controller = actions[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
path.shift()
|
actions.shift()
|
||||||
|
|
||||||
this.controller = controller
|
this.controller = controller
|
||||||
this.url = url
|
this.pathname = url
|
||||||
this.path = path
|
this.actions = actions
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -225,8 +221,8 @@ export default class Request {
|
||||||
|
|
||||||
get query() {
|
get query() {
|
||||||
if (!this.#query) {
|
if (!this.#query) {
|
||||||
let data = parse(this.#req.url).query
|
let { search } = urlParse(this.#req.url)
|
||||||
this.#query = querystring(data)
|
this.#query = querystring(search.slice(1))
|
||||||
}
|
}
|
||||||
return this.#query
|
return this.#query
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,22 @@
|
||||||
import { parse } from 'node:querystring'
|
import { parse } from 'node:querystring'
|
||||||
|
|
||||||
export function querystring(str) {
|
export function querystring(str) {
|
||||||
let query = parse(str)
|
let query = {...parse(str)}
|
||||||
|
|
||||||
for (let k of Object.keys(query)) {
|
for (let k of Object.keys(query)) {
|
||||||
let val = query[k]
|
let val = query[k]
|
||||||
|
|
||||||
if (k.endsWith('[]')) {
|
if (k.endsWith('[]')) {
|
||||||
let _k = k.slice(0, -2)
|
let _k = k.slice(0, -2)
|
||||||
|
if (query.hasOwnProperty(_k)) {
|
||||||
|
let old = query[_k]
|
||||||
|
if (!Array.isArray(old)) {
|
||||||
|
old = [old]
|
||||||
|
}
|
||||||
|
query[_k] = old.concat(val)
|
||||||
|
} else {
|
||||||
query[_k] = val
|
query[_k] = val
|
||||||
|
}
|
||||||
delete query[k]
|
delete query[k]
|
||||||
} else if (k.endsWith(']')) {
|
} else if (k.endsWith(']')) {
|
||||||
let idx = k.lastIndexOf('[')
|
let idx = k.lastIndexOf('[')
|
||||||
|
|
Loading…
Reference in New Issue