What are node.js bindings?
Asked Answered
L

3

29

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

Laryngology answered 4/12, 2013 at 17:51 Comment(3)
bindings are executable code that make V8 and the library able to talk to each other. An example is a binding for database libraries. V8 has no native understanding of databases, and database libraries don't care who uses them, so a binding is needed to make sure the two can talk to each other.Karlene
@Mike'Pomax'Kamermans , nice and straight forward, you should post it as an answer.Salgado
eh, it's not really the kind of question an SO answer makes sense for. You can find the same information with some googling, so I'll leave it as a comment instead. If it helps, great, if the question gets retracted, also great.Karlene
G
84

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!

Goring answered 27/5, 2016 at 18:30 Comment(4)
Very informative... how large is the binding component of these binding projects? When I look at an arbitrary "node bindings xxx" repo, it seems large with tons C/C++ files. Are these files just a copy of the original source, or have they been completely modified and partially re-written?Housebroken
Article link is deadWolters
I dont get it, your explanation seems a bit vague. Please add in the fact that our JS code gets interpredted into low level code and then executed.Selfjustifying
Article link is dead :(Optometer
H
9

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.

Hagerman answered 15/6, 2019 at 4:8 Comment(0)
L
1

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/

Laryngology answered 6/1, 2014 at 22:18 Comment(2)
the explanation in @Aren Li's answer is much better than the article you provide here. That article is rubbish in terms of explaining what bindings are.Fling
This adds fantastic in-depth info for a programmer to understand exactly how node.js bindings work in code. Personally I found @Aren Li's answer way too high level.Acheron

© 2022 - 2024 — McMap. All rights reserved.