美文网首页
3-3、node的一些实例

3-3、node的一些实例

作者: 懒羊羊3号 | 来源:发表于2019-12-19 17:30 被阅读0次

    1、新建一个get

    var express=require('express');
    var app =express();
    
    //设置跨域访问
    app.all('*', function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "X-Requested-With");
      res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
      res.header("X-Powered-By",' 3.2.1');
      res.header("Content-Type", "application/json;charset=utf-8");
      next();
    });
    
    var questions=[
      {
        data:213,
        num:444,
        age:12
      },
      {
        data:456,
        num:678,
        age:13
      }];
    
    //写个接口123
    app.get('/api/getGps',function(req,res){
      res.status(200),
        res.json(questions)
    });
    
    //配置服务端口 
    var server = app.listen(3000, function () {
      var host = server.address().address;
      var port = server.address().port;
      console.log('Example app listening at http://%s:%s', host, port);
    })
    // 前端
    $.getJSON('http://127.0.0.1:3000/api/getGps', function(data) {}
    

    2、循环读取文件夹的图片发送给前端

    var fs = require('fs')
    var app = require('express')();
    var webSocket = require('ws')
    var wss = new webSocket.Server({port:8080})
    
    var urls = '/Users/yangyangran/Downloads/图片收藏'
    
    var images = fs.readdirSync(urls)
    
    images.forEach(function (item, index) {
        console.log(item)
    })
    
    var index = 0;
    var len = images.length;
    wss.on('connection', function connection(ws) {
    
        setInterval(()=>{
            fs.readFile(`${urls}/${images[index]}`, 'binary',function (err, data) {
                if (err) {
                    return console.error(err);
                }
                console.log("异步读取: " );
                ws.send(data)
                index = (index + 1)%len;
                // const buf = Buffer.from(data, 'binary');
                // console.log(buf.toString('base64'));
                // ws.send(buf.toString('base64'));
            });
        }, 500)
    })
    app.listen(3000);
    // 前端
        var ws = new WebSocket('ws://localhost:8080');
        ws.onopen = function () {
            console.log('ws onopen');
        };
        ws.onmessage = function (e) {
            var url = "data:image/png;base64," + window.btoa(e.data);
            $('#show').html(`<img src=${url} alt="img">`)
        };
    

    3、按行读取并存入

    var fs = require('fs')
    var readline = require('readline')
    
    function readFileToArr(fReadName,callback){
      var fRead = fs.createReadStream(fReadName);
      var objReadline = readline.createInterface({
        input:fRead
      });
      var arr = new Array();
      objReadline.on('line',function (line) {
        arr.push(line);
        //console.log('line:'+ line);
      });
      objReadline.on('close',function () {
        // console.log(arr);
        callback(arr);
      });
    }
    
    var gpsData = []
    
    readFileToArr('/Users/yangyangran/Desktop/cve/1575026840.8389428-c1_gps.txt', data=>{
      data.forEach(x=>{
        if(x.indexOf('$GNRMC') > -1 || x.indexOf('$GPRMC') > -1){
          let fields = x.split(',')
          let lat = fields[3].slice(0,2)-0 + fields[3].slice(2)/60
          let lon = fields[5].slice(0,3)-0 + fields[5].slice(3)/60
          if(fields[3] && fields[5]){
            gpsData.push([lon, lat])
          }
        }
      })
    })
    

    相关文章

      网友评论

          本文标题:3-3、node的一些实例

          本文链接:https://www.haomeiwen.com/subject/anxolctx.html