【问题标题】:Why isn't fs loading my index.html file in my node.js application?为什么 fs 没有在我的 node.js 应用程序中加载我的 index.html 文件?
【发布时间】: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 @错误。

【问题讨论】:

  • 可以显示实际的运行日志吗? (在语法说明上,为什么要混合使用 constvar?你不会修改 httpfstime, so marking those const` 很有意义)
  • 感谢您的提示,我也将其更改为 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


【解决方案1】:

你调用了错误方法。 (考虑到你想要做什么)

readFileSync接受回调。

fs.readFileSync(path[, options])

你可能想要readFile方法

fs.readFile(路径[, 选项], 回调)

【讨论】:

  • 谢谢,我更新了它,所以我们调用 readFile。我现在正在查看我的日志,但仍然收到 502 网关错误。
  • 服务器在我的本地工作。 localhost:8000。也许你需要理顺一些其他的配置
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 2018-08-15
  • 2013-02-28
相关资源
最近更新 更多