Book Image

Mastering Node.js

By : Sandro Pasquali
Book Image

Mastering Node.js

By: Sandro Pasquali

Overview of this book

Node.js is a modern development stack focused on providing an easy way to build scalable network software. Backed by a growing number of large companies and a rapidly increasing developer base, Node is revolutionizing the way that software is being built today. Powered by Google's V8 engine and built out of C++ modules, this is a JavaScript environment for the enterprise.Mastering Node.js will take the reader deep into this exciting development environment. Beginning with a comprehensive breakdown of its innovative non-blocking evented design, Node's structure is explained in detail, laying out how its blazingly fast I/O performance simplifies the creation of fast servers, scalable architectures, and responsive web applications.Mastering Node.js takes you through a concise yet thorough tour of Node's innovative evented non-blocking design, showing you how to build professional applications with the help of detailed examples.Learn how to integrate your applications with Facebook and Twitter, Amazon and Google, creating social apps and programs reaching thousands of collaborators on the cloud. See how the Express and Path frameworks make the creation of professional web applications painless. Set up one, two, or an entire server cluster with just a few lines of code, ready to scale as soon as you're ready to launch. Move data seamlessly between databases and file systems, between clients, and across network protocols, using a beautifully designed, consistent, and predictable set of tools.Mastering Node.js contains all of the examples and explanations you'll need to build applications in a short amount of time and at a low cost, running on a scale and speed that would have been nearly impossible just a few years ago.
Table of Contents (20 chapters)
Mastering Node.js
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Appendix C. Creating your own C++ Add-ons

 

"If two (people) on the same job agree all the time then one is useless. If they disagree all the time, then both are useless."

 
 --Darryl F. Zanuck

A very common description of Node is this: Node.js allows JavaScript to run on the server. While true, this is also misleading. Node's creators organized and linked powerful C++ libraries in such a way that their efficiency could be harnessed without developers needing to comprehend their complexities. Node abstracts away the complexity of multiuser, simultaneous multithreaded I/O by wrapping that concurrency model into a single-threaded environment that is easy to understand and already well understood by millions of web developers.

When you are working with Node you are ultimately working with C++, a language whose suitability for enterprise-level software development no one would question.

Node's C++ foundation puts lie to claims that Node is not enterprise-ready. These claims confuse what JavaScript's role in the Node stack actually is. The bindings to Redis and other database drivers used regularly in Node programs are C bindings–fast, near the "metal". Node's process bindings (spawn, exec, and so on) facilitate a smooth integration of powerful system libraries (such as ImageMagick) with headless browsers and HTTP data streams. With Node, we are able to access the enormously powerful suite of native Unix programs through the ease and comfort of JavaScript. And of course, we can write our own C++ add-ons.

Paraphrasing Professor Keith Devlin's description in Calculus: One of the Most Successful Technologies (https://www.youtube.com/watch?v=8ZLC0egL6pc), these are some features of successful consumer technologies:

  • It should remove difficulty or drudgery from the process of completing a task.

  • It should be easy to learn and use.

  • It should be easier to learn and use than the popular method, if one exists.

  • Once learned, it can be used without constant expert guidance. A user remains able to remember and/or derive most or all of the rules governing interactions with the technology through time.

  • It should be possible to use it without knowing how it works.

Hopefully, as you think about the class of problems Node aims to solve, and the form of the solution it provides, the above five features are easily seen in the technology Node represents. Node is fun to learn and use, with a consistent and predictable interface. Importantly, "under the hood" Node runs enormously powerful tools that the developer only needs to understand in terms of their API.

Wonderfully, Node, V8, libuv, and the other libraries composing the Node stack are open sourced, a significant fact that further distinguishes Node from many competitors. Not only can one contribute directly to the core libraries but one can cut and paste code blocks and other routines to use in one's own work. You should see your growth into a better Node developer as a chance to simultaneously become a better C++ programmer.

This is not a primer on C++, leaving you to pursue this study on your own. Don't be intimidated! The C-family of languages is designed using forms and idioms not unlike what you are already used to with JavaScript. For example, if..then constructs, for loops, functions, and returning values from functions, even semicolon-terminated lines, are central to JavaScript and regularly used in C++ programming. As a JavaScript developer you should be able to understand the design and goals of the following examples with little effort, and can dip into C++ programming to resolve the meaning of the parts that aren't clear. Extending these examples iteratively is an excellent way to gently enter the world of C++ programming.