01-event-1.js

  1. const http = require("http");
  2. let request = {
  3. get url() {
  4. return this.req.url;
  5. },
  6. };
  7. let response = {
  8. get body() {
  9. return this._body;
  10. },
  11. set body(val) {
  12. this._body = val;
  13. },
  14. };
  15. let context = {
  16. get url() {
  17. return this.request.url;
  18. },
  19. get body() {
  20. return this.response.body;
  21. },
  22. set body(val) {
  23. this.response.body = val;
  24. },
  25. };
  26. class Application {
  27. constructor() {
  28. // this.callback = () => {}
  29. this.context = context;
  30. this.request = request;
  31. this.response = response;
  32. this.middlewares = [];
  33. }
  34. use(callback) {
  35. this.middlewares.push(callback);
  36. // this.callback = callback;
  37. }
  38. compose(middlewares) {
  39. return function (context) {
  40. return dispatch(0);
  41. function dispatch(i) {
  42. let fn = middlewares[i];
  43. if (!fn) {
  44. return Promise.resolve();
  45. }
  46. return Promise.resolve(
  47. fn(context, function next() {
  48. return dispatch(i + 1);
  49. })
  50. );
  51. }
  52. };
  53. }
  54. listen(...args) {
  55. const server = http.createServer(async (req, res) => {
  56. let ctx = this.createCtx(req, res);
  57. // await this.callback(ctx);
  58. const fn = this.compose(this.middlewares);
  59. await fn(ctx);
  60. ctx.res.end(ctx.body);
  61. // this.callback(req, res);
  62. });
  63. server.listen(...args);
  64. }
  65. createCtx(req, res) {
  66. let ctx = Object.create(this.context);
  67. ctx.request = Object.create(this.request);
  68. ctx.response = Object.create(this.response);
  69. ctx.req = ctx.request.req = req;
  70. ctx.res = ctx.response.res = res;
  71. return ctx;
  72. }
  73. }
  74. module.exports = Application;

02-stream-1.js

  1. var fs = require("fs");
  2. var readStream = fs.createReadStream("./data/file1.txt"); // 读取文件的 Stream
  3. var length = 0;
  4. readStream.on("data", function (chunk) {
  5. length += chunk.toString().length;
  6. });
  7. readStream.on("end", function () {
  8. console.log(length);
  9. });

02-stream-2.js

  1. var readline = require("readline");
  2. var fs = require("fs");
  3. var rl = readline.createInterface({
  4. input: fs.createReadStream("./data/file1.txt"),
  5. });
  6. var lineNum = 0;
  7. rl.on("line", function (line) {
  8. lineNum++;
  9. });
  10. rl.on("close", function () {
  11. console.log("lineNum", lineNum);
  12. });

02-stream-3.js

  1. var http = require("http");
  2. function serverCallback(req, res) {
  3. var method = req.method.toLowerCase(); // 获取请求的方法
  4. if (method === "get") {
  5. }
  6. if (method === "post") {
  7. // 接收 post 请求的内容
  8. var data = "";
  9. req.on("data", function (chunk) {
  10. // “一点一点”接收内容
  11. console.log("chunk", chunk.toString());
  12. data += chunk.toString();
  13. });
  14. req.on("end", function () {
  15. // 接收完毕,将内容输出
  16. console.log("end");
  17. res.writeHead(200, { "Content-type": "text/html" });
  18. res.write(data);
  19. res.end();
  20. });
  21. }
  22. }
  23. http.createServer(serverCallback).listen(8081); // 注意端口别和其他 server 的冲突
  24. console.log("监听 8081 端口……");

03-genarator-1.js

  1. function* helloWorldGenerator() {
  2. yield "hello";
  3. yield "world";
  4. return "ending";
  5. }
  6. var hw = helloWorldGenerator();
  7. console.log(hw.next());
  8. console.log(hw.next());
  9. console.log(hw.next());
  10. console.log(hw.next());
  11. function* foo() {
  12. yield 1;
  13. yield 2;
  14. yield 3;
  15. yield 4;
  16. yield 5;
  17. return 6;
  18. }
  19. for (let v of foo()) {
  20. console.log(v);
  21. }

application.js

  1. const http = require("http");
  2. let request = {
  3. get url() {
  4. return this.req.url;
  5. },
  6. };
  7. let response = {
  8. get body() {
  9. return this._body;
  10. },
  11. set body(val) {
  12. this._body = val;
  13. },
  14. };
  15. let context = {
  16. get url() {
  17. return this.request.url;
  18. },
  19. get body() {
  20. return this.response.body;
  21. },
  22. set body(val) {
  23. this.response.body = val;
  24. },
  25. };
  26. class Application {
  27. constructor() {
  28. // this.callback = () => {}
  29. this.context = context;
  30. this.request = request;
  31. this.response = response;
  32. this.middlewares = [];
  33. }
  34. use(callback) {
  35. this.middlewares.push(callback);
  36. // this.callback = callback;
  37. }
  38. compose(middlewares) {
  39. return function (context) {
  40. return dispatch(0);
  41. function dispatch(i) {
  42. let fn = middlewares[i];
  43. if (!fn) {
  44. return Promise.resolve();
  45. }
  46. return Promise.resolve(
  47. fn(context, function next() {
  48. return dispatch(i + 1);
  49. })
  50. );
  51. }
  52. };
  53. }
  54. listen(...args) {
  55. const server = http.createServer(async (req, res) => {
  56. let ctx = this.createCtx(req, res);
  57. // await this.callback(ctx);
  58. const fn = this.compose(this.middlewares);
  59. await fn(ctx);
  60. ctx.res.end(ctx.body);
  61. // this.callback(req, res);
  62. });
  63. server.listen(...args);
  64. }
  65. createCtx(req, res) {
  66. let ctx = Object.create(this.context);
  67. ctx.request = Object.create(this.request);
  68. ctx.response = Object.create(this.response);
  69. ctx.req = ctx.request.req = req;
  70. ctx.res = ctx.response.res = res;
  71. return ctx;
  72. }
  73. }
  74. module.exports = Application;

server.js

  1. // const http = require("http");
  2. // const server = http.createServer((req, res) => {
  3. // res.writeHead(200);
  4. // res.end("Hello WuChenDi");
  5. // });
  6. // server.listen(9092, () => {
  7. // console.log("server start on port 9092");
  8. // });
  9. const Dia = require("./application");
  10. const app = new Dia();
  11. function delay() {
  12. return new Promise((reslove, reject) => {
  13. setTimeout(() => {
  14. reslove();
  15. }, 2000);
  16. });
  17. }
  18. app.use(async (ctx, next) => {
  19. ctx.body = "1";
  20. await next();
  21. ctx.body += "2";
  22. });
  23. app.use(async (ctx, next) => {
  24. ctx.body += "3";
  25. await delay();
  26. await next();
  27. ctx.body += "4";
  28. });
  29. app.use(async (ctx, next) => {
  30. ctx.body += "5";
  31. });
  32. // app.use((req, res) => {
  33. // res.writeHead(200);
  34. // res.end("Hello WuChenDi");
  35. // });
  36. app.listen(9092, () => {
  37. console.log("server runing on port 9092");
  38. });

test.js

  1. // let digot = {
  2. // _name: "Di-got",
  3. // get name() {
  4. // // console.log('value')
  5. // // return '你好'
  6. // return this._name;
  7. // },
  8. // set name(val) {
  9. // console.log("new name is " + val);
  10. // this._name = val;
  11. // },
  12. // };
  13. // console.log(digot.name);
  14. // digot.name = "wuchendi";
  15. // console.log(digot.name);
  16. // function add(x, y) {
  17. // return x + y;
  18. // }
  19. // function double(z) {
  20. // return z * 2;
  21. // }
  22. // const res1 = add(1, 2);
  23. // const res2 = double(res1);
  24. // console.log(res2);
  25. // const res3 = double(add(1, 2));
  26. // console.log(res3);
  27. // function add(x, y) {
  28. // return x + y;
  29. // }
  30. // function double(z) {
  31. // return z * 2;
  32. // }
  33. // const middlewares = [add, double];
  34. // let len = middlewares.length;
  35. // function compose(midds) {
  36. // return (...args) => {
  37. // // 初始值
  38. // let res = midds[0](...args);
  39. // for (let i = 1; i < len; i++) {
  40. // res = midds[i](res);
  41. // }
  42. // return res;
  43. // };
  44. // }
  45. // const fn = compose(middlewares);
  46. // const res = fn(1, 2);
  47. // console.log(res);
  48. async function fn1(next) {
  49. console.log("fn1");
  50. await next();
  51. console.log("end fn1");
  52. }
  53. async function fn2(next) {
  54. console.log("fn2");
  55. await delay();
  56. await next();
  57. console.log("end fn2");
  58. }
  59. async function fn3(next) {
  60. console.log("fn3");
  61. }
  62. function delay() {
  63. return new Promise((reslove, reject) => {
  64. setTimeout(() => {
  65. reslove();
  66. }, 2000);
  67. });
  68. }
  69. function compose(middlewares) {
  70. return function () {
  71. return dispatch(0);
  72. function dispatch(i) {
  73. let fn = middlewares[i];
  74. if (!fn) {
  75. return Promise.resolve();
  76. }
  77. return Promise.resolve(
  78. fn(function next() {
  79. return dispatch(i + 1);
  80. })
  81. );
  82. }
  83. };
  84. }
  85. const middlewares = [fn1, fn2, fn3];
  86. const finalFn = compose(middlewares);
  87. finalFn();