How can I download image using GridFSBucket? - node.js

I tried everything I found on internet but nothing solved my problem. I really need help on this problem.
I'm following the tutorial "How to upload/store images in MongoDB using Node.js, Express & Multer" (https://www.bezkoder.com/node-js-upload-store-images-mongodb/).
I successfully uploaded the images. I can see the images on my navigator if I use the url from my backend (http://localhost:8080/chat/files/9f722635-bffc-4d4c-aa71-340c4a488ac2.jpeg). However, I can't use this url to display the image on my frontend (react native).
The problem doesn't seem to be related to my frontend, as I can access images from any other url with the same code. Moreover, when I tried to make a GET request on the postman with the same url, the image is not displayed in the body of the response.
My code is exactly the same of the tutorial.

Related

Node Express: How to request data and not a page

I am learning Node.js. This is a very newbie question.
I want to fetch some data from my database via node.js server. I am using Axios and so my line of code looks like this. But it is just an ordinary fetch request.
const response = api.get("/");
But I do not retrieve the data with this. I retrieve a completely new page that looks like this.
It is an empty page with the data that I want to retrieve. But I do not need a new page. I need the data only. I want to store it in the RESPONSE variable. How can I do that?
Can anyone point me in the right direction? Perhaps I should watch a video on how to build a REST API?
This is not a page. It is your browser's way of rendering the data of your response. You can use a JSON viewer plugin for your browser if you expect to see pretier data or use an HTTP client like Postman to be sure that your API returns what you expect.
So this was the problem.
My React was on port 3000. My Node was also on port 3000. When I started my React App, everything was fine and my browser displayed all pages perfectly well. But once I started my Node, it rendered data into the browser.
Just now I changed the Node port to 4000 and the problem is gone.
It seems like React and Node were competing here who is going to render the content into my browser. Node was prefered by my chrome browsers. Perhaps because I started it later.

What is the right way to upload images to your website Node js

Am not saying that i dont know how because i tried it before using multer and it works perfectly fine but the thing is these images get saved to a foldet called uploads with a namd and an id that i generatr using js
What i want to know : is this the propper way of doing it and if so wherr does that folder go to after hosting your website and does it contain alll the images from my website, i mean it will be a really large folder so where does it go to when hosting ??
PS: And another thing whenever i go to youtube or facebook etc i see a domain like i.ytimg.com containing these images so how is that acheived
I don't know if my reply would be helpful, but the way I use it is I save the image in a mongoDB database (Buffer) and just load it from the database.

Node.js and Vue.js, why Refresh make vue.js's store clear? And how can i use uploaded image in vue.js?

Title is my questions.
First. I'm making simple diary app with Node.js and Vue.js. Vue-router using history mode and in backend, using "connect-history-api-fallback" module. I think i did everything i can do, but when i run my app in local, refresh make vue.js's store clear. I googled but can't find same problem. Someone have any idea?
Second. I'm using Multer to upload. Upload is fine, i can see uploaded image. But i don't know how to show that uploaded image.
I mean in vue's template, what path will show uploaded image?
Image uploaded here "/simple-diary/backend/upload/profilePhoto/"
Vue.js component is here "/simple-diary/frontend/src/components/"
#samayo is right.One question per post.So of course when the page refresh the store is refreshed.So if you dont want that you can use plugin like vuex-persistedstate.Or if you want to keep the token and the user that is currently logged then when the user logged in,use localstorage to save the token and the user.Or cookies.Or session storage.You have many options

Image urls change while scraping in Node (works in browser console)

I'm using artoo.js for web scraping however For some reason the scraped image url's change when working with cheerio in node . i.e the original image url is :
"https://images-na.ssl-images-amazon.com/images/M/MV5BNWU4NmY3MTMtMTBmMi00NjFjLTkwMmItYWZhZWUwNDg5M2ExXkEyXkFqcGdeQXVyNDUyOTg3Njg#._V1_SX300.jpg"
However after scraping the Url turns to this url:
"http://ia.media-imdb.com/images/G/01/imdb/images/nopicture/156x231/tv-3797070466._CB522736147_.png#._V1_SX300.jpg"
If I scrape it while in chrome browser console using Artoo.js bookmark. The Url stays same as original.
Why is it changing when i use it in node?.Any Suggestions
UPDATE: Update: I think I found the issue but not the solution. It seems the scraper method runs before the correct images have loaded on page. the changed URL is just the placeholder image. How can I wait till the entire page loads.
It may be caused by some JS code. If you are using request+cheerio to scrap the page. When you make the request in node the JS code does nothing (it's not interpreted). So you are probably getting the original url before any lib or piece of code changes it. Try to look at the source code of the page in the browser Crtl+u. If it's "http://ia.media-imdb.com/images/G/01/imdb/images/nopicture/156x231/tv-3797070466._CB522736147_.png#._V1_SX300.jpg" then you will know some piece of code is doing something to change it.
Edit
If you absolutly need to run the JS to obtain the URL. You sould use phantomjs. It's a headless browser. The imaes will load. You can use it directly from nodejs or if you want a simpler way go with casperjs. I assume you're not used to scraping complicated web apps. If it's the case would go with casperjs. It's easy and it does the job. It's not as fast as using request + cheerio but it works. And you can put your code to run on a server.

Uploading an image to file system using Node.js

I am trying to upload an image to my server through a node.js server using express. I am in the process of writing an CRUD API but I am stuck on how to POST and save the image in a directory on my server.
//post
app.post('/public/media', function(req,res){
});
This is the barebones of my post method. I am trying to store the image in my media file. How should I go about this?
Note, I am not trying to store the image into a database. Rather, I am trying to store the image in a folder on my server and simply store the path to the image in my database.
Without duplicating an entire article about this issue, checkout this tutorial out. You will need to make slight adjustments to this code if using Express 4, otherwise it will work great.
Comment if you have issues below.

Resources