【问题标题】:Trouble writing a file with CronJob in Heroku using writeFileSync()使用 writeFileSync() 在 Heroku 中使用 CronJob 写入文件时遇到问题
【发布时间】:2018-01-12 23:54:46
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

  • 文件夹结构是什么,您还看到控制台日志吗?
  • 是的,我看到了控制台日志。 server.js 文件位于我的计算机上一个名为 node 的文件夹中,该文件夹还包含 procfile 和子文件夹 config/pullfiles

标签: node.js bash heroku cron


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多