Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are node.js bindings?

Tags:

node.js

v8

libuv

I am very new to node.js and I can not seem to find a definition anywhere as to what node.js bindings are. I have seen this term used in slides and nodejs talks but it was never clearly explained. Can anyone help clarify this concept for me? I have attached a picture of what I am referring to.enter image description here

like image 995
n3wb Avatar asked Dec 04 '13 17:12

n3wb


People also ask

What is Node JS repository?

2011-08-26. npm is two things: first and foremost, it is an online repository for the publishing of open-source Node. js projects; second, it is a command-line utility for interacting with said repository that aids in package installation, version management, and dependency management. A plethora of Node.

What is node js how it works?

It is a used as backend service where javascript works on the server-side of the application. This way javascript is used on both frontend and backend. Node. js runs on chrome v8 engine which converts javascript code into machine code, it is highly scalable, lightweight, fast, and data-intensive.

How does node JS runtime work?

Node. js is a JavaScript runtime environment that achieves low latency and high throughput by taking a “non-blocking” approach to serving requests. In other words, Node. js wastes no time or resources on waiting for I/O requests to return.


3 Answers

Rather than understanding what node.js bindings are, it is more useful to understand what "bindings" are in the first place.

Let's say you are writing a web application where a node.js (JavaScript) backend:

  1. receives requests from clients,
  2. conducts queries to databases,
  3. sorts the query results and finally
  4. returns the results to the client.

Now normally you would write all the code yourself. However, you know that there is an excellent sorting library that can take care of step 3 (i.e. sorting query results). The only problem is that the library is written in a system programming language such as C/C++ whereas your code is written in JavaScript. Normally you can't use that library in your code because they are in different programming languages, but with bindings, you can.

Bindings basically are libraries that "bind" two different programming languages so that code written in one language can be used in code written in another library. With the presence of bindings, you don't have to write all the code again just because they are in different languages. Another motivation for bindings is that you can benefit from the advantages of different programming languages. For example, C/C++ are much faster than JavaScript. It might be beneficial to write some code in C/C++ for performance purposes.

Now let's take a look at the picture you attached. V8 engine, according to Google Official website, is "written in C++". libuv adds a layer of abstraction that provides asynchronous I/O operations, written in C. However, the core functionalities of Node.js, such as networking, Database queries, file system I/O, are provided in libraries (or modules if you prefer) that are written in JavaScript. Plus, your code is written in JavaScript as well. Now in order for these pieces of technology written in different programming languages to communicate with each other, you have to "bind" them together, using bindings. These bindings are node.js bindings.

I've written an article lately that explains the architecture of Node.js' internal codebase where I explained how binds fit into Node.js!

like image 96
Aren Li Avatar answered Sep 27 '22 20:09

Aren Li


Node.js bindings are series of methods that can be used in Node.js code which are in reality just running C++ code behind the scenes.

fs.readFile()   

This method is not part of javascript. It's provided to v8 as part of the node.js runtime. So javascript does not know how to read a file from disk but C++ does. So when we use javascript code and node.js to read a file from disk it just defers all of that to the C++ function that can actually read the file from disk and get the results back.

enter image description here

Javascript also has bindings in the browser too. for example;

document.querySelector() 

is not a javascript code. It is implemented by chrome V8 engine.

like image 25
Yilmaz Avatar answered Sep 27 '22 19:09

Yilmaz


Upon further research i've come across this article. I hope this helps anyone out:

http://pravinchavan.wordpress.com/2013/11/08/c-binding-with-node-js/

like image 37
n3wb Avatar answered Sep 27 '22 19:09

n3wb