NodeJS後臺

程式設計師哆啦A夢發表於2020-11-07

NodeJS後臺

後臺: 1.PHP 2.Java 3.Python

優勢 1.效能 2.跟前臺JS配合方便 3.NodeJS便於前端學習

https://nodejs.org/en/

image.png

image.png

image.png

1.切換碟符 e: 2.改變目錄 cd 目錄名 3.執行程式 node xxx.js

``` const http = require('http');

http.createServer(function(req, res){ // 前臺響應 res.write("dashucoding"); res.end(); });

// 監聽 // 埠 server.listen(123); ```

nodeJS——伺服器

http——協議 request 請求 response 響應

image.png

檔案操作:fs——File System http——模組

非同步 vs 同步 非同步——多個操作可以同時進行,前一次的操作沒完事,後一次也能開始 同步——一次一個

``` const fs=require('fs');

// readFile(檔名,回撥函式) readFile(檔名, function (err, data){}) writeFile(檔名, 內容, function (err){})

oBtn.onclick=function (){ alert('a'); };

alert('b'); ``` image.png

image.png

image.png

``` const http=require('http'); const fs=require('fs');

var server=http.createServer(function (req, res){ //req.url=>'/index.html' //讀取=>'./www/index.html' // './www'+req.url var file_name='./www'+req.url;

fs.readFile(file_name, function (err, data){ if(err){ res.write('404'); }else{ res.write(data); } res.end(); }); });

server.listen(8080); ```

``` const fs=require('fs');

//writeFile(檔名, 內容, 回撥) fs.writeFile("bbb.txt", "dda", function (err){ console.log(err); }); ```

``` const fs=require('fs');

//readFile(檔名, 回撥函式) fs.readFile('aaa.txt', function (err, data){ if(err){ console.log('讀取失敗'); }else{ console.log(data.toString()); } });

//fs.writeFile ```

buffer類用於二進位制資料的儲存提供一個快取區 settimeout函式用於指定時間到達執行一個指定函式,指定方法為從當前時刻之後多少毫秒 cleartimeout函式用於取消在settimeout函式內指定的函式的執行

var http = require('http'); http.createServer(function(req,res){ res.writeHead(200, {'Content-Type':'text/html'}); res.write('<head><meta charset="utf-8"/></head>'); res.end('你好\n'); }).listen(1234,"127.0.0.1");

``` var http=require('http');

http.createServer(function(req,res){ // 回撥函式中的程式碼 }) ```

image.png

//一行 white-space: nowrap; text-overflow: ellipsis; overflow: hidden; word-break: break-all; //兩行 text-overflow: -o-ellipsis-lastline; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical;

image.png

image.png

小程式簡單實現雙擊事件

data = { tabBar: [], // recommendList: [], floorList: [], banners: [], currentIndex: 1, canGetCoupon: false, // 觸控開始時間 touchStartTime: 0, // 觸控結束時間 touchEndTime: 0, // 最後一次單擊事件點選發生時間 lastTapTime: 0 }

getCoupon: function (e) { let that = this if (that.touchEndTime - that.touchStartTime < 350) { // 當前點選的時間 let currentTime = e.timeStamp let lastTapTime = that.lastTapTime // 更新最後一次點選時間 that.lastTapTime = currentTime // 如果兩次點選時間在300毫秒內,則認為是雙擊事件 if (currentTime - lastTapTime < 300) { // 成功觸發雙擊事件時,取消單擊事件的執行 // clearTimeout(that.lastTapTimeoutFunc) getCoupon({}).then(res => { this.canGetCoupon = false }) wx.showToast({ title: '優惠券領取成功', duration: 3000 }) } }

``` doubleClick(e){ //e.timeStamp:當前點選時的毫秒數; // this.touchStartTime: 儲存上一次點選時的毫秒數,預設0 if (e.timeStamp - this.touchStartTime < 300){ 雙擊,進入 }

    this.touchStartTime = e.timeStamp;
    單擊
}

```

/* if ((e.timeStamp - this.touchStartTime) < 100) { getCoupon({}).then(res => { this.canGetCoupon = false }) wx.showToast({ title: '領取成功', icon: 'none', duration: 3000 }) } this.touchStartTime = e.timeStamp */

// 觸控開始時間 touchStartTime: 0, // 觸控結束時間 touchEndTime: 0,
// 最後一次單擊事件點選發生時間 lastTapTime: 0, // 單擊事件點選後要觸發的函式 lastTapTimeoutFunc: null,

``` /// 雙擊 doubleTap: function(e) { var that = this // 控制點選事件在350ms內觸發,加這層判斷是為了防止長按時會觸發點選事件 if (that.touchEndTime - that.touchStartTime < 350) { // 當前點選的時間 var currentTime = e.timeStamp var lastTapTime = that.lastTapTime // 更新最後一次點選時間 that.lastTapTime = currentTime

  // 如果兩次點選時間在300毫秒內,則認為是雙擊事件
  if (currentTime - lastTapTime < 300) {
    console.log("double tap")
    // 成功觸發雙擊事件時,取消單擊事件的執行
    clearTimeout(that.lastTapTimeoutFunc);
    wx.showModal({
      title: '提示',
      content: '雙擊事件被觸發',
      showCancel: false
    })
  }
}

}, ```

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

控制檯,全域性作用域,全域性變數和全域性函式,事件處理機制以及事件環機制,怎麼使用node.js框架中提供的除錯工具。


請點贊!因為你的鼓勵是我寫作的最大動力!

吹逼交流群:711613774 

相關文章