2 http
response.write(string|buffer)可以调用0-n次
response.end(string|buffer) 方法。必须调用一次
response.setHeader('Content-Type', 'text/html;charset=utf-8') 防止中文乱码
1最简单的列子
const http = require('http'); const fs = require('fs') function readFileByPath(path) { return new Promise((resolve, reject) => { fs.readFile(path, (err, data) => { err ? reject(err) : resolve(data) }) }) } const server = http.createServer(async (request, response) => {
response.setHeader('Content-Type', 'text/html;charset=utf-8')
const data = await readFileByPath('./static/1.txt') response.write(data) //可以write 文件流 response.write(100 + '') response.write('你好') response.end('只能传string或者buffer(缓冲器)') }) server.listen(3000)