簡體   English   中英

為什么fs無法在node.js應用程序中加載index.html文件?

[英]Why isn't fs loading my index.html file in my node.js application?

我正在使用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錯誤的網關錯誤。

關於我在這里缺少什么的任何想法?

編輯:我將功能從readFileSync更新為readFile ,現在我看到我的日志,沒有引發任何錯誤,但是當我導航到我的應用程序時,仍然看到502 Bad Gateway錯誤。

好吧,您正在調用錯誤的方法。 (考慮您要做什么)

readFileSync 並不需要一個回調。

fs.readFileSync(path [,options])

您可能想要readFile方法

fs.readFile(path [,options],回調)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM