From dd27a08e6fdeed287c7e1d1494142b5f5ce1300a Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 3 Dec 2018 16:50:29 +0800 Subject: [PATCH] use the ability of `content-type` lib directly As we can see the doc of `content-type`: [https://github.com/jshttp/content-type#contenttypeparsereq](https://github.com/jshttp/content-type#contenttypeparsereq), it can deal with the original `req` directly and will also throw the exception when `Content-Type` is missing or unvalid. So in my opinion, we don't need `let type = this.get('Content-Type');` to get the type and `if (!type) return ''` to check if the type is unvalid. --- lib/request.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/request.js b/lib/request.js index 0b0b2625a..e075cbe0f 100644 --- a/lib/request.js +++ b/lib/request.js @@ -367,16 +367,12 @@ module.exports = { */ get charset() { - let type = this.get('Content-Type'); - if (!type) return ''; - try { - type = contentType.parse(type); + const { parameters } = contentType.parse(this.req); + return parameters.charset || ''; } catch (e) { return ''; } - - return type.parameters.charset || ''; }, /**