使用writeFileSync()在Heroku中使用CronJob编写文件时出现问题

时间:2017-08-05 00:13:12

标签: node.js bash heroku cron

我正在尝试使用cronjob重复更新文件。最终,这将变得更加复杂,但现在我想弄清楚我目前的问题。我知道下面的代码有点过于复杂,因为我在尝试解决问题时保留了基本结构。这是服务器文件:

// server.js

var express  = require('express');
var port     = process.env.PORT || 8080;
var http = require('http');
var fs      = require("fs");
var curtainup = require('./diagnoseleak.js');
var url     = require("url" );

var app = express();

// launch ======================================================================
app.listen(port);
//run the CronJob
var CronJob = require('cron').CronJob;
new CronJob('0 * * * * *', function() {
  console.log("running");
  var date = new Date();
  console.log("Ran at: "+date.getHours()+":"+date.getMinutes());
  curtainup.doitnow();
} , null, true, 'America/New_York');

以下是名为diagnoseleak.js的文件:

var fs      = require("fs");
var mostRecentLocation  = "./config/pullfiles/mostRecent55.txt";

module.exports = {
  doitnow: function(){
    var writethefile = function(){
      fs.writeFileSync(mostRecentLocation, "A file called mostRecent55 should be create with this text", { flag: 'w' });
      console.log("This should write to the console");
    }
    writethefile();
  }
}

从包含服务器文件的目录中,我将以下内容键入cmd:

git add .
git commit -m "adding files"
git push heroku master
heroku run bash

然后进入bash窗口我输入:

cd config/pullfiles
ls -l

AND ...不会出现名为mostRecent55.txt的文件。我在找错了地方吗?最终我希望能够更新文件,但我有一种感觉,我要么找错了这个mostRecet55.txt文件的位置,要么就是错误地编写文件。

1 个答案:

答案 0 :(得分:1)

heroku不允许您将文件写入存储应用的文件系统。您将需要使用某种插件,数据库或某种外部服务。唯一的例外似乎是/tmp,它只是临时存储