-4

I am getting my hands on Node.JS fetch to understand how it works and then use it.

I have installed node-fetch and followed some tutorials and saw some videos on the subject, but it is not yet all clear. Even if what is shown works, I still have questions. Here one I need to solve soon. What is the way to simply display the page in the browser?

For example in the code below, I use a fetch to access my site (https://mynicewebsite.example.com), possibly setting some options (headers ....), and then I can log a number of informations using console.log(). But how should I change the code if I simply want the contents of the site (https://mynicewebsite.example.com) to be displayed in the browser?

Just as if I had typed the address directly in the address area of the browser.

Here the code with the fetch call.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
   const makeFetchCall = async () => {
      const response = await fetch('https://mynicewebsite.com', {
         method: 'GET',
         headers: {
            'Authorization': 'Bearer blahfblahdblahzblah',
            //..... // possibly some other things
         }
      });
   }

   makeFetchCall();
</script>
</body>
</html>
Michel
  • 10,303
  • 17
  • 82
  • 179

1 Answers1

3

fetch is an API used to get some data from a URL and make it available to JavaScript. It is provided by browsers to JS running in webpages.

node-fetch is a library that makes that API available to Node.js.

There's no sign of Node.js anywhere in your code. You are using the browser version, not node-fetch.

If you want to display a page in the browser, don't use fetch. Navigate to it:

location = "http://example.com"
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • OK. Sorry for the misunderstanding. My real project is indeed Node JS. The way I wrote my post was to make the question simple to read and easy to understand. I just want to know how to get what I want with fetch, that's all. At this point I have made a fetch apparently working but I don't get the final display I expect. – Michel Apr 15 '20 at 09:35
  • 2
    If your real project is Node.JS, then what does the browser have to do with it? – Quentin Apr 15 '20 at 10:41
  • It has to do that when I use a certain route, I don't see what I expect. – Michel Apr 15 '20 at 12:47
  • 1
    Then maybe you should ask a question about redirecting when the user request a particular route from whatever HTTP library you are using. You should include a [mcve]. – Quentin Apr 15 '20 at 13:57