0

Update: This issue is related to azure-function-express

I am using express.js and azure-function-express. Below is the piece of code I am using to try to return a package.zip file to the call using res.sendFile()

var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
  console.log('Sending signed certificate');
  var options = {
    root: __dirname + '/public/'
  };

  var fileName = 'package.zip';
  res.sendFile(fileName, options, function (err) {
    if (err) {
      console.log(err);
      next(err);
    } else {
      console.log('Sent:', fileName);
    }
  });
})

module.exports = router

However, when I run it, I get the following error:

TypeError: Cannot read property 'length' of null

[10/11/2018 1:19:21 AM] Sending signed certificate
[10/11/2018 1:19:25 AM] Worker 97959602-60bb-4608-9f3b-8696a2ee7dd6 uncaught exception:  TypeError: Cannot read property 'length' of null[10/11/2018 1:19:25 AM]
[10/11/2018 1:19:25 AM] Worker 97959602-60bb-4608-9f3b-8696a2ee7dd6 exited with code 1
[10/11/2018 1:19:25 AM]     at ServerResponse._send (_http_outgoing.js:232:33)
[10/11/2018 1:19:25 AM]     at write_ (_http_outgoing.js:667:15)
[10/11/2018 1:19:25 AM]     at ServerResponse.write (_http_outgoing.js:617:10)
[10/11/2018 1:19:25 AM]     at ReadStream.ondata (_stream_readable.js:639:20)
[10/11/2018 1:19:25 AM]     at emitOne (events.js:116:13)
[10/11/2018 1:19:25 AM]     at ReadStream.emit (events.js:211:7)
[10/11/2018 1:19:25 AM] Language Worker Process exited.
[10/11/2018 1:19:25 AM]     at addChunk (_stream_readable.js:263:12)
[10/11/2018 1:19:25 AM] node exited with code 1

Not able to figure out what is wrong in my code.

Interesting observations

  • The code does not reach if (err) line at all
  • The browser gets hung, waiting for a response indefinitely

Thanks in advance.

TechiRik
  • 1,893
  • 6
  • 27
  • 37
  • I tested your codes, everything's fine except that you forgot the third argument of the route handler function, the `next` argument, however, this mistake doesn't relate to the error that you got – You Nguyen Oct 11 '18 at 02:39
  • are you sure the package.zip in the right path? The "__dirname + '/public/'" may return the Illegal path – sinbar Oct 11 '18 at 03:32
  • @sinbar, I ran the code in debug mode, copied the value of "root" and was able to retrieve the file from browser using that path. So the location isn't the problem. – TechiRik Oct 11 '18 at 04:56
  • can you please check : `res.sendFile('index1.html', { root: path.join(__dirname, '../public') });` reference link : https://stackoverflow.com/questions/25463423/res-sendfile-absolute-path – IftekharDani Oct 11 '18 at 06:06

0 Answers0