1

I'm testing Azure Node Functions locally on my Linux box.

When this is triggered:

module.exports = async function (context, req) {
    readFile = require('../SharedCode/readFile.js');
    filepath = __dirname + '/../bootstrap-HTML-page/static/simple.html'
    fs = require('fs');

    fs.readFile(filepath,function(error,content){
      if(error) {
        content = error;
      }
      if(content) {
        context.res = {
          status: 200,
          headers: {
                'Content-Type': 'text/html'
              },
          body: content
        }
      }
    })
};

Then I get an empty response.

However if I run this, then I get the expected response:

context.res = {
      status: 200,
      headers: {
            'Content-Type': 'text/html'
          },
      body: '<html> <body> <h1> test </h1> </body> </html>'
    }

I've checked the variable content and can see my HTML page in there. I can't figure out why it is not responding with the page.

EDIT: I think this is because the built in API fs can't handle promises and you need use something like promisify to make it work as an async function

pixie
  • 55
  • 8

2 Answers2

3

I think this answer explains why it isn't working

and

This one how to make it work as a normal (non-async) function

this is my amended code that now works:

var fs = require('fs');


module.exports = function (context, req) {
    var filepath = __dirname + '/../bootstrap-HTML-page/static/simple.html'
    fs.readFile(filepath, 'utf8', function (err, content) {
        if (err) {
            context.log.error(err);
            context.done(err);
        }
        //context.log(result.name);
        context.res = {
        status: 200,
        headers: {
              'Content-Type': 'text/html'
            },
        body: content
      };
        context.done();
    });
}
pixie
  • 55
  • 8
-1

The problem with your code is, even though you have added async in your function declaration, you forgot to wait for the asynchronous fs.readFile operation to complete via the await keyword. Since it doesn't exist, your code execution continues in a normal fashion, which reaches the end of the method and hence returns an empty response.

module.exports = async function (context, req) {
    readFile = require('../SharedCode/readFile.js');
    filepath = __dirname + '/../bootstrap-HTML-page/static/simple.html'
    fs = require('fs');

    await fs.readFile(filepath,function(error,content){
      if(error) {
        content = error;
      }
      if(content) {
        context.res = {
          status: 200,
          headers: {
                'Content-Type': 'text/html'
              },
          body: content
        }
      }
    })
};
Danyal Imran
  • 2,515
  • 13
  • 21
  • Thanks Danyal, I did try that previously and it still doesn't return anything. If I `console.log(content)` then I can see the contents of my page, I'd expect it to return either a promise or undefined, if it was still waiting on the disk read... – pixie Jan 09 '19 at 15:56