azure-function-express
azure-function-express copied to clipboard
sendFile fails with TypeError: Cannot read property 'length' of null
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
Looks like it's the same error as #21