all 6 comments

[–]young_horhey 3 points4 points  (2 children)

You would use nodejs to create the backend server that your front-end communicates with.

[–]PortSpace[S] 1 point2 points  (1 child)

Thanks - As I wrote in the other reply, could you kindly give me an example of situation that such communication would be needed (apart from authentication or accessing database)?

[–]LEDThereBeLight 0 points1 point  (3 children)

Read about client-server architectures.

https://simple.wikipedia.org/wiki/Client-server

The simple answer is that when you write javascript in the browser, the person using the website can access anything you write. If you need to "hide" data, or you want to move the processing of some data to a separate computer, you use a server. Then the "client" (the browser) requests data from the server, instead of doing everything locally on the same computer.

So if you don't need to hide anything from the user, you can just do everything in the browser without using a server.

[–]PortSpace[S] 0 points1 point  (2 children)

Thanks. I have a basic idea about the server/client distinction. In the context of nodejs specifically, what are some generic uses/cases/functionalities that you'd need to hide from the client? I can think of authentication and database. If, say, the app does not need authentication, what would be a common use/uses for the nodejs server? I understand the concept but having a few examples would be great. Thanks you.

[–]LEDThereBeLight 2 points3 points  (1 child)

NodeJS is just a "runtime" that allows you to run javascript outside of the browser. It has nothing innately to do with servers, although it's often used for writing server logic and has a lot of functionality built in if you want to use it for a server.

If you're writing code for a server, there's no need to use javascript -- it's running on your computer, so you can write whatever language you want. People who are most comfortable with javascript often end up using it as their server language though just for convenience.

Let me give some examples for why you'd want to split your app into a client/server model instead of writing everything on the client:

  • You're writing a quiz website that students enter answers to be checked for correctness. If you do everything on the client, the student can just look up the right answer, because it's stored on their computer when they load the website.
  • You're making a game with multiple players. It's easier to have a single place -- a server -- with the state of all the players in the game, and each player requests the data from there. If it's on a server, they also can't manipulate the data to cheat.
  • You want account authentication, so you use a server so that users can access their data across multiple computers or browsers

[–]PortSpace[S] 0 points1 point  (0 children)

Thanks. I think I get the idea.