Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS /Electron Confusion about client/server code

This is a beginner kind of question. I haven't used NodeJS before, so it's a bit confusing to me. I don't see a clear separation between server modules and client modules. It seems like I can use "npm" (Node's package manager) to install both client modules and server modules.

My question is specifically related to this page: http://electron.atom.io/docs/v0.36.8/api/synopsis/

It says that I can use Node modules on client side:

The renderer process is no different than a normal web page, except for the extra ability to use node modules:

    <!DOCTYPE html>
    <html>
    <body>
    <script>
      const remote = require('electron').remote;
      console.log(remote.app.getVersion());
    </script>
    </body>
    </html>

How does this make any sense? Node is running on the server side, how is the browser ("renderer" process as they call it) able to use Node's packages?

like image 634
SpaceMonkey Avatar asked Mar 05 '16 20:03

SpaceMonkey


People also ask

Is node js client or server-side?

Node. js is an open-source server-side Javascript run-time environment built on Chrome's JavaScript Engine(V8). Node. js is used for building fast and scalable applications and is an event driven, non-blocking I/O model.

CAN node JS run on client side?

js application. In client-side we mainly deal with DOM or web APIs like cookies, but these things don't exist in Node. Other reasons why we cannot use node modules at the client side is that the node uses the CommonJS module system while the browser uses standard ES Modules which has different syntax.

Does Electron need a server?

On a desktop app, you don't have to have a server. But with electron you can use node direct in you app. The easy way: With electron, think of NodeJS as a Toolbox to do cool stuff. Like Filesystem manipulation and forget the client and server thing.

What does node allow us to do with one of the client side languages?

Node. js allows developers to write JavaScript code on both the server and client side. Compared to other languages, Node. js code execution is faster.


1 Answers

main process & renderer != server & client

NodeJS is Server side, not client side.
On a desktop app, you don't have to have a server. But with electron you can use node direct in you app.

The easy way: With electron, think of NodeJS as a Toolbox to do cool stuff. Like Filesystem manipulation and forget the client and server thing.

(Things you cant do with jquery etc. but things you need for a Desktop-App)

Electron: Main Process and Renderer

The main process is for handling/creating BrowserWindows(Renderer) And for some communication from one renderer-Window to an other one. (maybe some other special stuff too)

The renderer is where you really run the most of your app. With node, you have all you need there. Easy debugging (chrome dev tools) all in one place.

Node example in renderer

You can require "fs" > NodeJS File System
To read Files or Folders from your Harddrive.

You can do this for a example in the html File (script tag) which is display by the Renderer/Browser window.


It was very confusing for me too and my answer is for sure not perfekt and fully right. But my tip is, try do everything in the renderer. When you have to use the main process you will notice that.

like image 113
Sebastian Avatar answered Oct 26 '22 07:10

Sebastian