简体   繁体   中英

What are node.js bindings?

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. 在此输入图像描述

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 (ie 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!

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/

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.

在此输入图像描述

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

document.querySelector()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM