Nodejs if file exists from different url - node.js

Is it possible to check if image exists using absolute path? I have 2 apps using one domain, but with different ports. I want to check if the file exists on other app. For example from www.domain.com:80 I want to check if this file exists: www.domain.com:8080/images/image1.jpg. I have tried to do it with fs.exists, fs.existsSync, fs.Stats, but it on the first 2 functions it always returns false and on the fs.Stats it returns error "ENOENT, no such file or directory", but I can view the file entering url on browser. How can I do it?

The fs functions look for files on your local filesystem. If you want to see if a remote file exists, you'll have to make an HTTP request using the http module, request module or the like.

Related

use getObject() from forge-api npm, how to make the return result as a download link?

I am using forge-api getObject() to download the excel from BIM360 hub. I set up express sever in the backend and make the call in the frontend.
I could get the result of the object and it looks like this:
So my question is:
How can I convert the result as a download link correctly? I could download the excel, but the excel can not be opened...
My code looks like this:
backend:
frontend:
I think all you need to modify in your backend code is to return content.body, instead of content
See e.g. https://github.com/Autodesk-Forge/forge-derivatives-explorer/blob/master/routes/data.management.js#L296
It might even be better if you generated a pre-signed URL for the file and passed that to the client. In that case, the file would not be downloaded to your server first and then to the client, but directly to the client in a single step.
https://forge.autodesk.com/en/docs/data/v2/reference/http/buckets-:bucketKey-objects-:objectName-signed-POST/

Can we read files from server path using any fs method in NodeJs

In my case I need to read file/icon.png from cloud storage/bucket which is a token base URL/path. Token resides in header of request.
I tried to use fs.readFile('serverpath') but it gave back error as 'ENOENT' i.e. 'No such file or directory' is existed, but file is existed on that path. So are these methods are eligible to make calls and read files from server or they work only with static path, if that is so then in my case how to read file from cloud bucket/server.
Here i need to pass that file-path to UI, to show this icon.
Use this lib to handle GCS operations.
https://www.npmjs.com/package/#google-cloud/storage
If you do need use fs, install https://cloud.google.com/storage/docs/gcs-fuse, mount bucket to your local filesystem, then use fs as you normally would.
I would like to complement Cloud Ace's answer by saying that if you have Storage Object Admin permission you can make the URL of the image public and use it like any other public URL.
If you don't want to make the URL public you can get temporary access to the file by creating a signed URL.
Otherwise, you'll have to download the file using the GCS Node.js Client.
I posted this as an answer as it is quite long to be a comment.

Watch folder for created file and upload created file to server using Node RED

Goal
I want to watch folder using node red. If any file created then I want to upload that file to server using node red http request node.
I have used node-red-contrib-wfwatch module for folder watch and http request module for api call.
Issue
I am able to get change event on file created but how can i pass select file to http request body as form data. I have no idea how to do it.
I am new in Node Red so can anyone please help for achieve this.
Here what I want to achieve
Structure of Node red display here
The node-red-contrib-wfwatch node doesn't get the file, just sends a message with following payload:
{
changeType: "update",
filePath: "/tmp/foo"
}
You need to use File node for this purpose. But before that, because the File node expects the filename be in msg.filename not in msg.payload.filePath you need to add Change node:
and connect the whole thing like this:
Just replace the Debug node with your HTTP Request one.

Server MapPath not working on remote server

I am using Server.MapPath to find the path for a document uploaded to a remote server, so that I can then open it. However when using it, it is returning a relative path and so rather than searching the remote server it is searching the local machine instead.
What I am using to open the document is:
System.Diagnostics.Process.Start(Server.MapPath(Path.Combine("~/", document)));
Where "document" is the part of the path relative to the document itself, in this case "Files\2016\11\doc_name". So I want to take the path of this document, go to the top level of the site, and then find the document from there.
However I would hope that this would return a path similar to "server\inetpub\site\Files\2016\11\doc_name" but instead it is returning a path like "d:\inetpub\site\Files\2016\11\doc_name".
Can someone help me with what is the correct function to use to get the path I need?
EDIT
I have managed to fudge together the correct path using the following code:
string server = Environment.MachineName;
string path = Server.MapPath(Path.Combine("~/", documentpath));
System.Diagnostics.Process.Start(#"\\" + server + path.Substring(path.IndexOf(#"\")));
However, while I can get this to access the file when I'm running the project locally, it errors when I try to do it on the published site. As I can access it in one way, I'm assuming that it could be permissions (just to note the site is using windows authentication). Is this the most likely cause?

Snap configuration file

How would I correctly use configuration file in Snap?
At the moment, I hard code DB host and DB name. If I wanted to put it in a file within projectroot/config directory, how would I make it available within a handler or within app init function?
It is mentioned briefly in snaplets tutorial that configurator library can be used but there was no explanation of how to actually use it.
Thanks.
Just call getSnapletUserConfig which returns a Config. Then use functions from configurator to get the information you need. Look at snaplet-postgresql-simple's use of config files for a working example.
The config file defaults to devel.cfg in the current snaplet file path. So if you are using getSnapletUserConfig in your top-level application, then the config file will be in your project root. Otherwise it will be in snaplets/foo where "foo" is the name of whatever snaplet you are in.

Resources