【发布时间】:2020-01-02 23:16:00
【问题描述】:
我正在使用 fs 从我的 index.js 文件中加载 index.html,但它不仅没有加载,而且我在 fs 函数调用中进行的测试也没有被记录。
这是我的整个 index.js 文件:
const cron = require("cron");
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
console.log("inside readFileSync, before error handling");
if (err) {
console.log("inside readFileSync, in error handling");
throw err;
}
console.log("inside readFileSync, past error handling");
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
// init global config
global.config = require("../src/util/config");
const MatchingBot = require("../src/MatchingBot");
var time = new Date();
console.log(time.getHours()+":"+time.getMinutes()+"________________");
new cron.CronJob({
cronTime: config.CRON_TIME,
onTick: MatchingBot.run,
start: true
});
这是我的日志:
> matching-bot@1.0.0 start /var/app/current
> node ./bin/index.js | bunyan
17:44________________
inside readFileSync, before error handling
inside readFileSync, past error handling
当我导航到我的应用时,我收到 502 bad gateway 错误。
关于我在这里缺少什么的任何想法?
编辑:我将函数从 readFileSync 更新为 readFile,现在我看到了我的日志,没有抛出错误,但是当我导航到我的应用程序时,我仍然看到 @987654325 @错误。
【问题讨论】:
-
可以显示实际的运行日志吗? (在语法说明上,为什么要混合使用
const和var?你不会修改http、fs或time, so marking thoseconst` 很有意义) -
感谢您的提示,我也将其更改为 const。我更新了它和函数调用,现在看到了我的日志,但 index.html 仍然没有呈现。
-
不过,您的帖子并未显示您的实际日志,因此请继续添加。话虽如此,502 bad gateway 不是由 Node 引起的:该错误准确地描述了问题(“you”和“node”之间的任何网关/代理都没有正确配置,因此您可能没有配置 aeb 来正确路由您的电话)
-
@Mike'Pomax'Kamermans 我添加了日志。这是一个很好的观点,我将尝试从这个角度进行更多的故障排除。
-
出于调试目的,您可能还想做的是将
http.createServer(...).listen(8000)更改为const server = http.createServer(...); server.listen(8000, () => { console.log(`server listening on ${server.address()}`)
标签: javascript node.js amazon-elastic-beanstalk