代码拉取完成,页面将自动刷新
'use strict'
const http = require('http')
const fs = require('fs')
/** ----------------------------解析Body数据-----------------------------*/
/**
* Content-Disposition: form-data;name="file";filename="xxx"
* Content-Type: text/plain
*
* @param {string} headerData
*/
function parseBodyHeaders(headerData){
let hlist = headerData.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'] || ''
}
}
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 headerData = bodyData.toString('utf8',crlf_bdy.length,header_end_index)
// 解析文件头信息
let hd = parseBodyHeaders(headerData)
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------------------------- */
/**
*
* .jpg .jpeg -> image/jpeg
*
* .png -> image/png
*
* .gif -> image/gif
*
* .ico -> image/x-icon
*
* .txt -> text/plain
*
* .json -> text/json 或 application/json
*
* .xml -> text/xml 或 application/xml
*
*/
let html = `
<!DOCTYPE html>
<html>
<head>
<title>First HTTP</title>
</head>
<body>
happy dragon
</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
console.log(status)
response.statusCode = status
console.log('test')
response.end('test end')
},
'/upload': async(reuqest,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)
})
console.log('data')
// let bodyData = null
let bodyData = html
// let bodyData = 'nodejs'
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.setHeader('content-type','text/html; charset=utf-8')
response.end(bodyData)
}else{
response.end()
}
})
}
}
}
// chunk:以Buffer类型储存的二进制数据
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。