1 Star 0 Fork 0

pei-mengmeng/小程序study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
upload.js 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
pei-mengmeng 提交于 2021-10-13 10:25 . 1
'use strict'
const http = require('http');
const fs = require('fs');
/**------------------------解析Body数据--------------------- */
/**
* Content-Disposition:form-data;name = "file";
*
* @param {string} headerDate
*/
function parseBodyHeaders(headerDate) {
let hlist = headerDate.split('\r\n').filter(p => p.length > 0)
//解析Content-Disposition => hlist[0]
//按照;切分数组,选出从第二个元素开始的数组
let csplit = hlist[0].split(';').slice(1)
let filename
let name
for (let a of csplit) {
if (a.indexOf('filename=') === 0) {
filename = a.substring(a.indexOf('=')+2,a.length - 1)
}
else {
name = a.substring(a.indexOf('=')+2,a.length - 1)
}
}
let headers = {}
let ind = 0;
let k
for(let h of hlist){
ind = h.indexOf(':')
k = h.substring(0,ind).toLowerCase()
headers[k] = h.substring(ind+1).trim()
}
return{
filename,
name,
headers,
contentType:headers['content-type']||[]
}
let contentType
}
function parseBody(bodyData, headers) {
let ctype = headers['content-type']
let bdy = ctype.substring(ctype.indexOf('=') + 1)
let crlf_bdy = `\r\n--${bdy}`
let header_end_index = bodyData.indexOf('\r\n\r\n', crlf_bdy.length)
let headerDate = bodyData.toString('utf8',crlf_bdy.length, header_end_index);
//解析文件头信息
let hd = parseBodyHeaders(headerDate)
let file = {
start: header_end_index + 4,
end: bodyData.indexOf(crlf_bdy, header_end_index),
...hd
}
file.length = file.end - file.start
return file
}
/**------------------------解析Body数据-END--------------------- */
let html = `
<!DOCTYPE html>
<html>
<head><title>first http</title>
</head>
<body>你好。</body>
</html>
`
let routerTable = {
GET: {
'/': async (request, response) => {
response.setHeader('content-type', 'text/html;charset=utf-8');
response.end(html);
},
'/test': async (request, response) => {
let status = parseInt(Math.random() * 200) + 199
response.statusCode = status
response.end('test end')
},
'/upload': async (request, response) => {
let stm = fs.createReadStream('./upload.html')
stm.pipe(response, { end: false })
stm.on('end', () => {
response.end()
})
},
},
POST: {
'/data': async (request, response) => {
let bufferList = [];
let bufferLength = 0;
request.on('data', chunk => {
bufferLength += chunk.length
bufferList.push(chunk)
})
let bodyData = null;
request.on('end', () => {
if (bufferList.length > 0) {
bodyData = Buffer.concat(bufferList, bufferLength)
bufferList = []
}
//
let file = parseBody(bodyData, request.headers)
console.log(file)
if (bodyData) {
response.end(bodyData)
} else {
response.end()
}
})
}
}
}
http.createServer((request, response) => {
let rm = routerTable[request.method]
if (!rm || !rm[request.url]) {
response.statusCode = 404
response.end('page not found')
return
}
rm[request.url](request, response)
})
.listen(3456);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pei-mengmeng/applet-study.git
git@gitee.com:pei-mengmeng/applet-study.git
pei-mengmeng
applet-study
小程序study
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385