Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendFile fails with TypeError: Cannot read property 'length' of null #24

Open
RitwikChatterjee opened this issue Oct 11, 2018 · 1 comment

Comments

@RitwikChatterjee
Copy link

I am trying to use azure-function-express in my code.

Here is my piece of code in index.js:

const createHandler = require("azure-function-express").createHandler;
const express = require("express");
var myRouter = require('./routes/myrouter.js');
const app = express();
app.use("/api/files", myRouter);
 
module.exports = createHandler(app);

Here is my myrouter.js:

var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
  console.log('Sending file');
  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

When I call this API, it fails at the res.sendFile(fileName,....) step with the following error.
Note: It doesn't even reach the if(err) step.

Sending file
Worker ce5e00ff-a507-4910-92cc-8ce08323c082 uncaught exception:  TypeError: Cannot read property 'length' of null
Worker ce5e00ff-a507-4910-92cc-8ce08323c082 exited with code 1
    at ServerResponse._send (_http_outgoing.js:232:33)
    at write_ (_http_outgoing.js:667:15)
    at ServerResponse.write (_http_outgoing.js:617:10)
    at ReadStream.ondata (_stream_readable.js:639:20)
    at emitOne (events.js:116:13)
    at ReadStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ReadStream.Readable.push (_stream_readable.js:208:10)
    at fs.read (fs.js:2051:12)
Debugger attached.
Waiting for the debugger to disconnect...
Language Worker Process exited.
node exited with code 1
 Worker ce5e00ff-a507-4910-92cc-8ce08323c082 uncaught exception:  TypeError: Cannot read property 'length' of null.

The problem goes away, if I change the index.js to just use express.js (without any other changes)
Updated index.js which is working:

const createHandler = require("azure-function-express").createHandler;
const express = require("express");
var myRouter = require('./routes/myrouter.js');
const app = express();
app.use("/api/files", myRouter);

app.listen(3030, ()=> console.log("Listening on port: ", 3030));

Can you please look into the issue

@lworkman
Copy link

Looks like it's the same error as #21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants