Wrote file to server


#1

Ok I needed to write a JPG data (image file) from a call to Google API to disk. I use the following:

fs.writeFile(’.streetview.jpg’, body, ‘binary’, function(err){
if (err) throw err
console.log(‘File saved.’)
})

I can run the Glitch console and do DIR and the file is there. I want to use the file for display in Messenger using JSON code. What is the path I should use in the JSON to grab the image and display in Messenger? I can’t figure out the URL of the file.

I also would like to know - is there a TEMP place to write this file? I only need to display and don’t need it later. So is there a directory that I can use that would be removed.


#2

It sounds like you’ve written the file to the root of the /app directory in your app. Depending on how you’ve setup your webserver it might not be accessible via a url in that location. I suggest creating a public directory and writing it to there. If you share your project name, we can take a look at your webserver code and can tell you more specifics.

If your project is private, either make it public or DM me a join link.

There’s a temp directory at /tmp.


#3

Thanks Gareth! I would like to DM the link but have no idea how to do that in here. I searched high and low - looked in Messages - I see now where to DM. Where do I find that? Really appreciate the help!


#4

If you click on my name you might see the option. However, if you’re new then you may not yet have permissions to do so. In that case, feel free to email me: gwilson@fogcreek.com


#5

Hey Garth

I sent an Email - thanks Jeff


#6

Here’s an example project which uses the Google Maps API to request a Streetview image (which returns jpeg/image encoded data) from a URL and saves it to the local filesystem in the public folder. public is set in Express to be where static assets are served from, and thus the image URL you could pass to Messenger is https://streetview-image.glitch.me/photo.png.


#7

Thanks Gareth I am going to getting going on this now I THANK YOU SO MUCH! You can’t believe the time I have spent on this small little thing. I am new to NODE so I am so thankful or this platform. I have 20+ years in programming but in COBOL and RPG.


#8

Hey Gareth I got it working in my project - thanks. I have one last thing though - I have the address from the user - they type in the street address and all else I need is the zip code - I don’t have the longtitude and latitude (I cold get it but thats another call).

I have the address and zip working but the process you gave me does not like spaces in the address. If I put %20 in there then we are fine. How can I cure the space issue error. Once I have this should be GOLD.


#9

Hey Gareth never mind on the spaces in address - got it done this way

var address = encodeURIComponent(request.query.streetAddress.trim());

Worked like a charm. Thanks for the help!


#10

Did have another question. Is NodeJS so much different than PHP in handling HTTP calls? The reason I ask is there is somebody that did the same thing - getting the image from Google and did it in PHP - he gets the URL from Google where the image is stored versus here seems in Node it passes back the raw jpg data. Would might know why?


#11

Not that I know of, perhaps they’re using a library or another API? The Google Maps API returns image data, not an URL to a file.


#12

So all is working GREAT! One thing I can’t get to work though is deleting of the file that is in the public directory. So I had to change the process a bit. I an going out and getting an image of a property. Now saving it to the /Public directory with the street name in it.

  1. Created the file called N108w14569_Bel_Aire_lane.png. I did a scan/replace to replace spaces with underscores. All works fine in the creation and display of it.

  2. Once it’s displayed in Messenger I wanted to delete it since now there will be a NEW file for each address looking for a value of their home. This is what I am trying to use to delete:

fs.unlink(‘https://purple-diploma.glitch.me/’ + replaceAdress + ‘.png’,function(err){
if(err) return console.log(err);
console.log(‘1’)
console.log(‘file deleted successfully’);
});

(Continued in the next post because the system won’t let me have more than 2 links in a post for NEW user)


#13

And this is the error I get:

{ Error: ENOENT: no such file or directory, unlink ‘https://purple-diploma.glitch.me/W151N10778_Preserve_PKWY.png
errno: -2,
code: ‘ENOENT’,
syscall: ‘unlink’,
path: ‘https://purple-diploma.glitch.me/W151N10778_Preserve_PKWY.png’ }

So I am not sure why. I will attach a screen shot as well of what the directory looks like. (The URLs above have a space in them after https: since the system would only let me as a new user have 2 links in my post.

THanks, Jeff


#14

fs is running on your server, so you need to provide the relative path to the file you want to delete and not the URL. So it’d likely be something like __dirname + "/public/" + replaceAddress + ".png"


#15

SWEET that did it! Thank you!