前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NodeJS后台

NodeJS后台

作者头像
达达前端
发布2019-07-03 10:36:29
1.8K0
发布2019-07-03 10:36:29
举报
文章被收录于专栏:达达前端达达前端

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

代码语言:javascript
复制
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 同步 异步——多个操作可以同时进行,前一次的操作没完事,后一次也能开始 同步——一次一个

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
代码语言:javascript
复制
const fs=require('fs');

//writeFile(文件名, 内容, 回调)
fs.writeFile("bbb.txt", "dda", function (err){
  console.log(err);
});
代码语言:javascript
复制
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函数内指定的函数的执行

代码语言:javascript
复制
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");
代码语言:javascript
复制
var http=require('http');

http.createServer(function(req,res){
// 回调函数中的代码
})

image.png

代码语言:javascript
复制
 //一行
    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

小程序简单实现双击事件

代码语言:javascript
复制
data = {
      tabBar: [],
      // recommendList: [],
      floorList: [],
      banners: [],
      currentIndex: 1,
      canGetCoupon: false,
      // 触摸开始时间
      touchStartTime: 0,
      // 触摸结束时间
      touchEndTime: 0,
      // 最后一次单击事件点击发生时间
      lastTapTime: 0
    }
代码语言:javascript
复制
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
            })
          }
        }
代码语言:javascript
复制
doubleClick(e){
    //e.timeStamp:当前点击时的毫秒数;
    // this.touchStartTime: 储存上一次点击时的毫秒数,默认0
        if (e.timeStamp - this.touchStartTime  < 300){
            双击,进入
        }

        this.touchStartTime = e.timeStamp;
        单击
    }
代码语言:javascript
复制
/*
        if ((e.timeStamp - this.touchStartTime) < 100) {
          getCoupon({}).then(res => {
            this.canGetCoupon = false
          })
          wx.showToast({
            title: '领取成功',
            icon: 'none',
            duration: 3000
          })
        }
        this.touchStartTime = e.timeStamp
        */
代码语言:javascript
复制
  // 触摸开始时间
  touchStartTime: 0,
  // 触摸结束时间
  touchEndTime: 0,  
  // 最后一次单击事件点击发生时间
  lastTapTime: 0, 
  // 单击事件点击后要触发的函数
  lastTapTimeoutFunc: null, 
代码语言:javascript
复制
  /// 双击
  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框架中提供的调试工具。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NodeJS后台
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档