【发布时间】: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