thy-util-expressapp 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +44 -0
- package/package.json +17 -0
package/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const express = require("express")
|
|
2
|
+
// 通过以下方式引入文件
|
|
3
|
+
// const { app, getReqPayload } = require("thy-util-expressApp")
|
|
4
|
+
|
|
5
|
+
const app = express()
|
|
6
|
+
|
|
7
|
+
app.use(express.text())
|
|
8
|
+
app.use(express.json())
|
|
9
|
+
app.use(express.urlencoded({ extended: true }))
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 处理 req 的 body 内容,根据 Content-Type 进行不同的处理
|
|
13
|
+
* @param {object} req - 请求。
|
|
14
|
+
* @returns {string} 文本形式的req.body。
|
|
15
|
+
*/
|
|
16
|
+
function getReqPayload(req) {
|
|
17
|
+
const contentType = req.headers['content-type']
|
|
18
|
+
let reqPayload = null
|
|
19
|
+
|
|
20
|
+
if (!contentType || req.method.toUpperCase() === "GET") {
|
|
21
|
+
return reqPayload
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (req.body) {
|
|
25
|
+
if (contentType.includes('text/plain')) {
|
|
26
|
+
reqPayload = req.body
|
|
27
|
+
} else if (contentType.includes('application/json')) {
|
|
28
|
+
reqPayload = JSON.stringify(req.body)
|
|
29
|
+
} else if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
30
|
+
reqPayload = JSON.stringify(req.body)
|
|
31
|
+
} else if (contentType.includes('multipart/form-data')) {
|
|
32
|
+
logger2console('multipart/form-data 需要 multer 处理')
|
|
33
|
+
reqPayload = 'multipart/form-data (需要额外处理)'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return reqPayload
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
app,
|
|
42
|
+
getReqPayload
|
|
43
|
+
}
|
|
44
|
+
module.exports.default = { app }
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "thy-util-expressapp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "this is thy-util-expressApp",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"thy-util-expressApp"
|
|
11
|
+
],
|
|
12
|
+
"author": "hengyi.tao",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"express": "^5.2.1"
|
|
16
|
+
}
|
|
17
|
+
}
|