test.js

  1. // 标准输入输出 (管道)
  2. // process.stdin.pipe(process.stdout);
  3. // const http = require("http");
  4. // const server = http.createServer((req, res) => {
  5. // if (req.method === "POST") {
  6. // req.pipe(res);
  7. // }
  8. // });
  9. // server.listen(8800);
  10. // 复制文件
  11. // const fs = require("fs");
  12. // const path = require("path");
  13. // const fileName1 = path.resolve(__dirname, "data.txt");
  14. // const fileName2 = path.resolve(__dirname, "data-bak.txt");
  15. // const readStream = fs.createReadStream(fileName1);
  16. // const writeStream = fs.createWriteStream(fileName2);
  17. // readStream.pipe(writeStream);
  18. // readStream.on("data", (chunk) => {
  19. // console.log(chunk.toString());
  20. // });
  21. // readStream.on("end", () => {
  22. // console.log("copy done");
  23. // });
  24. const http = require("http");
  25. const fs = require("fs");
  26. const path = require("path");
  27. const fileName1 = path.resolve(__dirname, "data.txt");
  28. const server = http.createServer((req, res) => {
  29. if (req.method === "GET") {
  30. const readStream = fs.createReadStream(fileName1);
  31. readStream.pipe(res);
  32. }
  33. });
  34. server.listen(8800);