前端技术前沿9

刺骨的言语ヽ痛彻心扉 2021-09-19 14:16 434阅读 0赞

image.png

image.png

image.png

Node.js使用Module模块去划分不同的功能,以简化应用的开发。

  1. var myModule = require('./myModule.js');

将某些方法和变量暴露到模块外,可以使用exports对象去实现。

安装
Linux 下 Node.js的安装

  1. sudo apt-get update
  2. sudo apt-get install node
  3. 或者:
  4. sudo aptitude update
  5. sudo aptitude install node

image.png

  1. var http = require('http');
  2. server = http.createServer(function (req, res) {
  3. res.writeHeader(200, {"Content-Type": "text/plain"});
  4. res.end("Hello");
  5. });
  6. server.listen(8000);
  7. console.log("httpd start @8000");

image.png

  1. console.log("Hello World");
  2. node helloworld.js

image.png

Node.js 安装配置

image.png

image.png

install-node-msi-version-on-windows-step1

install-node-msi-version-on-windows-step2

install-node-msi-version-on-windows-step3

install-node-msi-version-on-windows-step5

引入required模块
创建服务器
接收请求与响应请求

步骤一、引入required模块

  1. var http = require("http");

步骤一、创建服务器

使用http.createServer()方法创建服务器,并使用listen方法绑定8888端口。 函数通过request, response参数来接收和响应数据。

双击

  1. // 触摸开始时间
  2. touchStartTime: 0,
  3. // 触摸结束时间
  4. touchEndTime: 0,
  5. // 最后一次单击事件点击发生时间
  6. lastTapTime: 0,
  7. // 单击事件点击后要触发的函数
  8. lastTapTimeoutFunc: null,
  9. /// 按钮触摸开始触发的事件
  10. touchStart: function(e) {
  11. this.touchStartTime = e.timeStamp
  12. },
  13. /// 按钮触摸结束触发的事件
  14. touchEnd: function(e) {
  15. this.touchEndTime = e.timeStamp
  16. },
  17. /// 单击
  18. tap: function(e) {
  19. var that = this
  20. wx.showModal({
  21. title: '提示',
  22. content: '单击事件被触发',
  23. showCancel: false
  24. })
  25. },
  26. /// 双击
  27. doubleTap: function(e) {
  28. var that = this
  29. // 控制点击事件在350ms内触发,加这层判断是为了防止长按时会触发点击事件
  30. if (that.touchEndTime - that.touchStartTime < 350) {
  31. // 当前点击的时间
  32. var currentTime = e.timeStamp
  33. var lastTapTime = that.lastTapTime
  34. // 更新最后一次点击时间
  35. that.lastTapTime = currentTime
  36. // 如果两次点击时间在300毫秒内,则认为是双击事件
  37. if (currentTime - lastTapTime < 300) {
  38. console.log("double tap")
  39. // 成功触发双击事件时,取消单击事件的执行
  40. clearTimeout(that.lastTapTimeoutFunc);
  41. wx.showModal({
  42. title: '提示',
  43. content: '双击事件被触发',
  44. showCancel: false
  45. })
  46. }
  47. }
  48. },
  49. 单击、双击和长按同时存在的实现:
  50. /// 长按
  51. longTap: function(e) {
  52. console.log("long tap")
  53. wx.showModal({
  54. title: '提示',
  55. content: '长按事件被触发',
  56. showCancel: false
  57. })
  58. },
  59. /// 单击、双击
  60. multipleTap: function(e) {
  61. var that = this
  62. // 控制点击事件在350ms内触发,加这层判断是为了防止长按时会触发点击事件
  63. if (that.touchEndTime - that.touchStartTime < 350) {
  64. // 当前点击的时间
  65. var currentTime = e.timeStamp
  66. var lastTapTime = that.lastTapTime
  67. // 更新最后一次点击时间
  68. that.lastTapTime = currentTime
  69. // 如果两次点击时间在300毫秒内,则认为是双击事件
  70. if (currentTime - lastTapTime < 300) {
  71. console.log("double tap")
  72. // 成功触发双击事件时,取消单击事件的执行
  73. clearTimeout(that.lastTapTimeoutFunc);
  74. wx.showModal({
  75. title: '提示',
  76. content: '双击事件被触发',
  77. showCancel: false
  78. })
  79. } else {
  80. // 单击事件延时300毫秒执行,这和最初的浏览器的点击300ms延时有点像。
  81. that.lastTapTimeoutFunc = setTimeout(function () {
  82. console.log("tap")
  83. wx.showModal({
  84. title: '提示',
  85. content: '单击事件被触发',
  86. showCancel: false
  87. })
  88. }, 300);
  89. }
  90. }
  91. },
  92. var http = require('http');
  93. http.createServer(function (request, response) {
  94. // 发送 HTTP 头部
  95. // HTTP 状态值: 200 : OK
  96. // 内容类型: text/plain
  97. response.writeHead(200, {'Content-Type': 'text/plain'});
  98. // 发送响应数据 "Hello World"
  99. response.end('Hello World\n');
  100. }).listen(8888);
  101. // 终端打印如下信息
  102. console.log('Server running at http://127.0.0.1:8888/');

请点赞!因为你的鼓励是我写作的最大动力!

官方微信公众号

吹逼交流群:711613774

吹逼交流群

发表评论

表情:
评论列表 (有 0 条评论,434人围观)

还没有评论,来说两句吧...

相关阅读

    相关 前端技术前沿10

    允许用户从NPM服务器下载别人编写的第三方包到本地使用。 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。 允许用户将自己编写的包或命令行程序上传到NP