带有标志'a'的nodejs fs.openSync()无法运行java程序

时间:2016-10-11 09:25:52

标签: javascript node.js

我遇到的问题是我制作的Twitter Bot,涉及一个处理-java程序来动态生成图像,在EC2服务器上运行:成功工作几天后,我开始从终端获取以下错误消息因为程序不再生成图像。

想知道如何解决这个问题并让我的机器人重回正轨。有什么建议吗?

fs.js:432
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT, no such file or directory 'postcards/output.png'
    at Object.fs.openSync (fs.js:432:18)
    at Object.fs.readFileSync (fs.js:286:15)
    at processing (/home/ubuntu/bot.js:141:29)
    at ChildProcess.exithandler (child_process.js:641:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:743:16)
    at Socket.<anonymous> (child_process.js:956:11)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Pipe.close (net.js:466:12)
ubuntu@ip-172-31-46-35:~$ 

这是我调用生成图像并发布推文的主要功能:

var exec = require('child_process').exec; 
var fs = require('fs'); 


function tweetIt(txt) {
    //Making this program launch processing sketch and execute it // I just need to use node to invoke this file in the terminal $ node bot.js
    //var cmd = 'processing-java --sketch=`pwd`/postcards --run';
    var cmd = 'postcards/postcards'; 
    exec(cmd, processing); // command to execute the sketch and callback function to call when done

    function processing() {
        var filename = "postcards/output.png";
        var params = {
            encoding: 'base64'
        }
        var b64content = fs.readFileSync(filename, params);
        T.post('media/upload', {
            media_data: b64content
        }, uploaded); // just uplading my image to twitter before being able to tweet it
        console.log("done with the image!");

        function uploaded(err, data, response) {
            // this is where the tweet including the image happens
            var id = data.media_id_string;
            var tweet = {
                status: txt,
                media_ids: [id]
            }
            T.post('statuses/update', tweet, tweeted); // type, params, callbackfunction
        }

        function tweeted(err, data, response) {
            if (err) {
                console.log(err);
            } else {
                console.log("It works!");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

路径问题?尝试使用__dirname(当前文件的路径)

的相对路径
fs.readFile(__dirname+"/../myfolder/picture.png",...)