1 Star 0 Fork 0

陌鹍/study2021wechat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
first-http.js 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
kunbeicheng 提交于 2021-09-29 09:05 . first
'use strict'
const http = require('http')
/**
*
* .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>
雷猴啊。
</body>
</html>
`
let routerTable = {
'/' : 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')
},
'/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 = []
}
if(bodyData){
response.end(bodyData)
} else{
response.end()
}
})
}
}
http.createServer((request, response) => {
if(!routerTable[request.url]) {
response.statusCode = 404
response.end('page not found')
return
}
routerTable[request.url](request, response)
})
.listen(3456)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/m-o-k-u-n/study2021wechat.git
git@gitee.com:m-o-k-u-n/study2021wechat.git
m-o-k-u-n
study2021wechat
study2021wechat
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385