0

Hey guys i'm from php background and i'm trying to learn nodeJs currently but i'm bit confused about fetching data from database .

Problem 1 : How does NodeJs communicate with Database under the hood ?

Problem 2: Why do we need to make REST API in order to do backend stuff (Fetch data ,delete , add data) ? can't we do it like the way we do it in php -> connect to the database and do our backend stuff (Fetch data ,delete , add data).

Yes i know what API is and how it works .

I have read this answers : answer but this does not have what i'm looking for .

sorry if that question looks dumb :)

Any help would be helpful Thanks

Ajay
  • 1
  • 1

2 Answers2

0

I don't really know what you expect from the first part so I'll try to answer the second one.

You don't need to do a RESTful API if you don't want to with NodeJs. It's just a good way to structure your API and make it as easy to use as possible.

The NodeJs API, which is running on server side, offers end-points to other applications to access your data. It can just be an Interface between your database and other apps like your React/Angular front, or other APIs for exemple.

You can also create a NodeJs app like you would with php doing everything from serving Html to directly communicating with the database.

Reda.a
  • 26
  • 2
  • my question was how nodejs do it under the hood since it is not server side language , – Ajay Mar 26 '20 at 16:17
  • Node is a JavaScript execution environment, not a language, that is used server side not client side. Browsers have their own JavaScript environnement. – Reda.a Mar 27 '20 at 09:10
  • If you want to read more about it there is a good article that is more complete and might answer your question more in depth [here](https://www.freecodecamp.org/news/the-definitive-node-js-handbook-6912378afc6e/) – Reda.a Mar 27 '20 at 09:16
-1

Node .js runs on the client side, in the user's browser. But the database is on the server.

The two sides communicate by means of AJAX, which can be referred-to as a "remote procedure call" mechanism: the server receives the request, prepares the result, encodes them (usually) as JSON, and sends the results back to the client.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
  • thanks for answer but nodeJs is not a server side language than how it do it under the hood ? – Ajay Mar 26 '20 at 16:11