代码拉取完成,页面将自动刷新
'use strict'
const http = require('http')
let html = `
<!DOCTYPE html>
<html>
<head>
<title>First Server</title>
</head>
<body>
<div>Node.js in service.</div>
</body>
</html>
`
let routerTable = {
'/' : async (request, response) => {
response.setHeader('content-type',
'text/html; charset=utf-8')
response.end(html)
},
'/x/y' : async (request, response) => {
response.end('-- ok --')
},
'/x/y/z' : async (request, response) => {
let bufferList = []
let totalLength = 0
request.on('data', chunk => {
totalLength += chunk.length
bufferList.push(chunk)
})
let bodyData = null
request.on('end', () => {
if (bufferList.length > 0)
bodyData = Buffer.concat(bufferList, totalLength);
bufferList = [];
if (bodyData) {
response.setHeader('content-type',
'text/plain; charset=utf-8')
response.end(bodyData)
} else {
response.end()
}
})
}
}
let serv = http.createServer((request, response) => {
if (routerTable[ request.url ] === undefined) {
response.statusCode = 404
response.end('page not found')
return
}
routerTable[ request.url ](request, response)
})
serv.listen(3456)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。