Resolve path when sending example file for Windows support

Previously, when trying to import an example into the flow editor on
Windows, the load attempt would fail with an HTTP 404 error in the
browser client, with a `TypeError: path must be absolute or specify
root to res.sendFile` error being written to the Node-RED log. This was
due to the path being passed to the `res.sendFile` function not being
fully-qualified (for example, `\Users\myuser\...\example.json`).

With the changes in this commit, the path to the example file is
resolved to a fully-qualified path before being passed into the
`res.sendFile` call. For example, a path on Windows of
`\Users\myuser\...\example.json` would be transformed to
`C:\\Users\\myuser\\...\\example.json` before being passed along to the
`sendFile` function. This change allows the file to be loaded and sent
properly to the browser client and for the embedded flows in the example
to be loaded in the flow editor.
This commit is contained in:
Jeremy Barlow 2018-09-19 08:44:26 -07:00
parent 08fccc4e77
commit cb35604ef5
2 changed files with 6 additions and 4 deletions

View File

@ -102,9 +102,10 @@ module.exports = {
var fullPath = redNodes.getNodeExampleFlowPath(module,path);
if (fullPath) {
try {
fs.statSync(fullPath);
var resolvedPath = fspath.resolve(fullPath);
fs.statSync(resolvedPath);
log.audit({event: "library.get",type:"flow",path:req.params[0]},req);
return res.sendFile(fullPath,{
return res.sendFile(resolvedPath,{
headers:{
'Content-Type': 'application/json'
}

View File

@ -225,7 +225,7 @@ describe("api/editor/library", function() {
throw err;
}
res.body.should.have.property('sendFile',
'node-module:example-one');
fspath.resolve('node-module') + ':example-one');
done();
});
});
@ -243,7 +243,8 @@ describe("api/editor/library", function() {
throw err;
}
res.body.should.have.property('sendFile',
'@org_scope/node_package:example-one');
fspath.resolve('@org_scope/node_package') +
':example-one');
done();
});
});