1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| var http = require("http") var fs = require("fs")
var server = http.createServer()
server.on("request", (req, res) => { var url = req.url
if (url === "/plain") { res.setHeader("Content-Type", "text/plain; charset=utf-8") res.end("你好,世界!") } else if (url === "/html") { res.setHeader("Content-Type", "text/html; charset=utf-8") res.end("<h1>Hello, World!</h1>") } else if (url === "/file") { fs.readFile('./05.jpg', (err, data) => { if (err) { res.setHeader("Content-Type", "text/plain; charset=utf-8") res.end("文件读取失败!") } else { res.setHeader("Content-Type", "image/jpeg") res.end(data) } }) } })
server.listen(3000, () => { console.log("server is listening in port 3000...") })
|