how I can prevent res.sendFile changing file path? - node.js

I'm using node.js and want to send file to the frontend. So I specified the direct path to my file like:
path = "c:/app/A"
and when I run res.sendFile(path, fileName);
I'm getting the Error: ENOENT: no such file or directory, stat '/home/projects/c:/app/A'
How I can disable this auto path adding "/home/projects" part?
I want to download file that is not in my project folder with my code. File is in my computer in different folder.

Try to use \\ as path delimiter for Windows (c:\\app\\A) and read about Node.js module "path".

so I need use just new URL(file:${"c:/app/A"});
so it will be like that:
let filename = "someName.com"
let absPath = "c:/app/someName.com";
fs.writeFileSync(`${filename}`, fs.readFileSync(new URL(`file:${absPath}`)));
res.download(`${filename}`, `${filename}`)

Related

AWS Lambda access denied to a module in subfolder

I have this Nodejs lambda function where some files are in a subfolder, like this:
- index.js
- connectors/
- affil.js
I have a Cannot find module error when trying to require the affil.js file. Trying to read it with fs.readFile returns an access denied error.
When I move the file to the root folder, it is accessible. Is there a requirement that Lambda functions files must all be at the root directory? How can I fix that?
Mostly it is because of the way zipping the files making the problem. Instead of zipping the root folder you have to select all files and zip it like below,
Please upload all files and subfolders like below. Please include node_modules folder as well in the zip.
As pointed by #Vijayanath Viswanathan, the issue is with how the zip file is created rather than Lambda.
I used to feed gulp-zip with this:
var src = gulp.src('src/**/*')
The correct way is to prevent folders from being included:
var src = gulp.src('src/**/*.js')
or (if you need to include file with other file extensions)
var src = gulp.src('src/**/*', {nodir: true})

How to get filepath of a file in Azure Service Fabric

I have a project which is in an Azure Service Fabric Solution. How can I get specific full filepath of a content file? The content file is in the same folder with my source code.
What I tried:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location
But it turns out:
C:\SfDevCluster\Data_App_Node_4\ABCXYZType_App126\ABCXYZPkg.Code.1.0.0\ABCXYZ.dll
This is a file in bin/debug folder
To get the location of content files you can use:
var path = Path.Combine(
FabricRuntime.GetActivationContext().GetCodePackageObject("Code").Path,
"Readme.txt");
ServiceEventSource.Current.ServiceMessage(this.Context, File.ReadAllText(path));
provided that the file Readme.txt has the Build Action is set to "Content" and the Copy to Output Directory setting is set to something else than "do not copy".

Relative addressing files python3

I have variable address = /data/train/1.jpg, and I'm trying to read file by
im = Image.open(address)
FileNotFoundError: [Errno 2] No such file or directory: '/data/train/1.jpg'
By some reasons I can't use full name of file.
I started jupyter notebook from folder which actually contains file 1.jpg in /data/train/.
How can I fix it?
Relative addressing means from the perspective of the current working directory. So if you script is in the same directory that the data folder is in, your path to the file would be ./data/train/1.jpg. Note the ./, which means the current directory.
use relative path, this one is absolute
address = './data/train/1.jpg'
im = Image.open(address)
in this case . means current location while slash means the environments root (view and explanation) on this depends on your OS

Resolving paths in a custom root directory and avoiding going in parent folders

Using path.resolve we can resolve paths using Node.js.
I want to have a directory containing files and other directories and so on. From the client I receive a path as string and I have to resolve it in this directory.
Because of security, I have to prevent the result path to be outside of the directory:
// This comes from the client
let filePath = "/../../../some-sensitive-data-from/disk";
/* Here I have to check if the path is inside of the directory */
path.resolve(ABSOLUTE_PATH_TO_MY_DIR + filePath);
How to prevent the access to parent directories of ABSOLUTE_PATH_TO_MY_DIR?
Obviously a solution would be to check if the final path is still inside of the ABSOLUTE_PATH_TO_MY_DIR path. But maybe there are other better solutions.

Require file somewhere in the directory node.js

I have a file that is required in many other files, that are on different folders, inside the main directory.
Is there a way to just require the filename without having to write the relative path, or the absolute path? Like require('the_file'). And without having to go to npm and install it?
Create a folder inside your main directory , put the_file.js inside and set the NODE_PATH variable to this folder.
Example :
Let's say you create a ./libs folder within your main directory, you can just use :
export NODE_PATH = /.../main/lib
after that, you can require any module inside this directory using just :
var thefile = require('the_file')
To not have to do that every time, you'd have to add the variable to your .bashrc (assuming you're running a Unix system).
Or you can set a global variable inside your app.js file and store the path of your 'the_file' in it like so :
global.rootPath = __dirname;
Then you can require from any of your files using :
var thefile = require(rootPath+'/the_file')
These are the most convenient methods for me, short of creating a private npm, but there are a few other alternatives that I discovered when looking up an answer to your question, have a look here : https://gist.github.com/branneman/8048520

Resources