diff --git a/index.js b/index.js index c8558ae..38e8818 100644 --- a/index.js +++ b/index.js @@ -42,9 +42,13 @@ export default class Response { this.set('Content-Type', mime) } + set length(val) { + this.set('Content-Length', val) + } + set body(buf = null) { if (this.#ended) { - return this + return } this.#ended = true @@ -77,9 +81,6 @@ export default class Response { if (this.#ended) { return } - if (!/^(http[s]?|ftp):\/\//.test(url)) { - url = '//' + url - } this.set('Location', url) this.status = f ? 301 : 302 this.body = null @@ -89,10 +90,10 @@ export default class Response { * [location 页面跳转(前端的方式)] */ location(url) { - let html = `
` if (this.#ended) { return } + let html = `` this.render(html) } @@ -101,13 +102,14 @@ export default class Response { if (this.#ended) { return } - data += '' - data = data || STATUS_TEXT[code] - this.type = 'html' - this.set('Content-Length', Buffer.byteLength(data)) if (code) { this.status = code } + data += '' + data = data || STATUS_TEXT[this.status] + this.type = 'html' + this.set('Content-Length', Buffer.byteLength(data)) + this.body = data } @@ -176,9 +178,8 @@ export default class Response { * @param {Num} code [返回码] * @param {Str} msg [提示信息] * @param {Str/Obj} data [额外数据] - * @param {Str} callback [回调函数名] */ - send(code = 200, msg = '', data, callback) { + send(code = 200, msg = '', data) { let output if (this.#ended) { @@ -186,20 +187,13 @@ export default class Response { } if (msg && typeof msg === 'object') { - callback = data data = msg msg = STATUS_TEXT[code] } else { msg = msg || STATUS_TEXT[code] } - output = { code, msg, data } - output = JSON.stringify(output) - - if (callback) { - callback = callback.replace(/[^\w\.]/g, '') - output = callback + '(' + output + ')' - } + output = JSON.stringify({ code, msg, data }) this.type = 'json' this.set('Content-Length', Buffer.byteLength(output)) @@ -231,6 +225,11 @@ export default class Response { return this } + delete(key) { + this.#res.removeHeader(key) + return this + } + /** * [append 往header插入信息] * @param {String} key [description]