Home > Software engineering >  Node Express: How to request data and not a page
Node Express: How to request data and not a page

Time:10-27

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.

browser window with retrieved data

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related