简体   繁体   中英

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

I am using fs to load an index.html from my index.js file, but not only is it not loading, but the testing I put inside the fs function call isn't being logged either.

Here is my entire index.js file:

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
});

Here are my logs:

> 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

When I navigate to my app, I get a 502 bad gateway error.

Any ideas on what I'm missing here?

EDIT: I updated the function from readFileSync to readFile , and I see my logs now, no error is thrown, but still when I navigate to my application I see a 502 Bad Gateway error.

Well you're calling the wrong method. (considering what you're trying to do)

readFileSync does not take a callback.

fs.readFileSync(path[, options])

You probably want readFile method

fs.readFile(path[, options], callback)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM