标签:16px content listen parse 环境 web static == creates
已经安装 node 环境。
var http = require(‘http‘); var path = require(‘path‘); var fs = require(‘fs‘); var url = require(‘url‘); var server = http.createServer((req, res) => { var staticPath = path.join(__dirname, ‘‘); var pathObj = url.parse(req.url, true); if (pathObj.pathname == ‘/‘) pathObj.pathname += ‘index.html‘; var filePath = path.join(staticPath, pathObj.pathname); // 异步读取文件数据 fs.readFile(filePath, ‘binary‘, (err, fileContent) => { if (err) { res.writeHead(404, ‘Not Found‘); res.end(‘<h1>404 Not Found</h1>‘); } else { res.writeHead(200, ‘ok‘); res.write(fileContent, ‘binary‘); res.end(); } }) }) server.listen(8012); console.log(‘server is ok!‘)
缺点:1.暂不支持热更新
标签:16px content listen parse 环境 web static == creates
原文地址:https://www.cnblogs.com/cc-freiheit/p/12367196.html