• EBADF, read


    nodejs读取文件出的一个错误,解决不了,自己技术还达不到,解决不了这么高深的问题。

    描述:需要记录访问的人数,每个人随机到的酒。打算用json文件来存储:read count write count; read user write user,结果出现了如下问题。

    网上能找到的相关答案:http://www.tuicool.com/articles/2Qn2Un

    以后的打算:

    1、尽量避免用file来存储,需要频繁并发访问的数据;

    2、nodejs一出错就挂了,所以需要好好研究下异常处理

    错误提示:

    events.js:85
          throw er; // Unhandled 'error' event
                ^
    Error: EBADF, read
        at Error (native)
    

    错误代码: 

    var express = require('express');
    var router = express.Router();
    var fs = require('fs');
    var path = require('path');
    var util = require('util');
    
    var wine_data = [
        {
            "name":"熊猫精酿生姜金色艾尔",
            "money":50,
            "reason":"暖心向,极品云备胎",
            "reason-1":"备胎不易。赏50元,来熊猫精酿喝杯",
            "reason-2":"别暖了TA,冷了自己",
            "href":"http://weidian.com/item.html?itemID=1623850670&wfr=wx&from=singlemessage&isappinstalled=1",
            "image":"shengjiang.png"
        }
    ];
    
    function selectResult(ran, res, uname){
        if(ran < 3){        
            res.render('result', {
                uname: uname,
                wname: wine_data[ran].name,
                reason: wine_data[ran].reason,
                reason1: wine_data[ran]["reason-1"],
                reason2: wine_data[ran]["reason-2"],
                money: wine_data[ran].money,
                href: wine_data[ran]["href"]
            });
        }else{                
            res.render('result-n', {
                uname: uname,
                wname: wine_data[ran].name,
                reason: wine_data[ran].reason            
            });
        }
    }
    
    /* GET home page. */
    router.get('/', function(req, res, next) {
    
        var count_path = path.join(process.cwd(), '/public/data/count.json');
    
        fs.readFile(count_path, function(err, data){
            if(err){
                console.log('err:'+err);
            }else{
                // console.log('data:'+data);
            }
    
            var data_json = JSON.parse(data);
            var count = data_json.count;
            count++;
    
            res.render('index', {
                count:count
            });
    
            data_json.count = count;
    
            fs.writeFile(count_path, JSON.stringify(data_json, function(key, value){return value;}, 4), function(err){
                
                if(err){
                    console.log('add count fail:'+err);
                }else{
                    // console.log('del success');
                }
    
            });   //writeFile end
    
        });  //readFile end
        
    });
    
    router.get('/result', function(req, res, next) {
    
        var user_path = path.join(process.cwd(), '/public/data/user.json');
        var uname = req.query.uname;
    
        fs.readFile(user_path, function(err, data){
            if(err){
                console.log('err:'+err);
            }else{
                // console.log('data:'+data);
            }
    
            var data_json = JSON.parse(data);        
            if(data_json[uname] !== undefined){
                // 存在
                var ran = +data_json[uname];
    
                selectResult(ran, res, uname);            
    
            }else{
                // 不存在
                var ran = parseInt(data.length*Math.random());
    
                data_json[uname] = ran;
    
                selectResult(ran, res, uname);            
    
                fs.writeFile(user_path, JSON.stringify(data_json, function(key, value){return value;}, 4), function(err){
                
                    if(err){
                        console.log('add user fail:'+err);
                    }else{
                        // console.log('add user success');
                    }
    
                });   //writeFile end
    
            }// 是否存在 结束       
    
        });  //readFile end
    
    });
    
    module.exports = router;
    
  • 相关阅读:
    理解字节序(转)
    《逆向分析实战》数据的存储及表示形式
    C语言——内存分配
    C语言编程基础学习字符型数据的ASCII码值为何是负数?
    你知道嵌入式C语言中各变量存储的位置吗?
    stm32入门(从51过渡到32)
    说说M451例程讲解之LED
    说说M451的例程库的说明
    STM32总线结构和存储器
    STM32学习之路入门篇之指令集及cortex——m3的存储系统
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4969668.html
Copyright © 2020-2023  润新知