The popularity of Node.js just keeps surging, it's great for so many things. But what if you need serious C/C++ speed? What if you want to move to Node.js, but can't afford to rewrite your existing C++ code?
Fear not... you can overcome your speed or legacy code problem by writing native C++ addons for Node.js! Once you know how, you can call your C++ code right from your Node.js code - easily and cleanly.
Learning the complex C++ API for addon development is hard. Answers are spread across a few blog posts and terse documentation. If you are looking for a step-by step and simple explanation, you'll be looking for a long time! Worse yet - since the V8 API changes so frequently, it's hard to blend all these resources together in a comprehensible and consistent way.
That's what this book is all about - I've put together a comprehensive manual for creating your C++ addon. Ranging from simple synchronous examples, asynchronous C++ modules and object oriented designs, to defending yourself against changing API's - "C++ and Node.js Integration" will get you to the finish line. And it will save you weeks of research time!
Sign up to for my newsletter on Node.js integration. With your signup, you'll get a free copy of Chapter 2 - Understanding V8 Programming!
This book is a major expansion on a four-part series on my blog on building C++ addons for Node.js. I've expanded each of the topics into full chapters, and added four completely new chapters. The book also contains more code examples, and coverage of the latest V8 API changes.
Learn the basics of creating Node.js Addons and how to use node-gyp to create and test them. We'll go through a few quick examples, and outline the rest of the book.
Learn about the C++ underpinnings of Node.js and how it all works. You'll see why Node's interface with C++ is so efficient, and how V8's design effects how you write your addons. In this chapter we'll cover all the V8 data types, and how the V8 memory system can be accessed from C++.
See how to create your first full-featured C++ addon and pass JavaScript objects to C++. We'll look at turning your JavaScript objects into first-class objects matching a C++ class definition and how to return data back to Node.js.
What good is a Node.js module if it blocks your event loop? In this chapter you'll learn how to do the heavy computational task you've written in C++ asynchronously. Just give your C++ code a JavaScript callback function, and your JavaScript can just keep on running until the C++ is done.
If you have existing C++ classes that you want to be able to use as "native" JavaScript objects, then you'll need to learn to wrap your C++ classes with V8 data structures.
The V8 API has undergone many "breaking" changes - and since different version of Node.js use different versions of V8, the situation can get pretty complex to manage. This chapter will show you how to work at a higher level of abstraction using Nan - the Native Abstractions for Node.js library. Using Nan, you'll defend yourself from the changing V8 API.
Learn how to build great interfaces to your C++ addon by supporting an event emitter and streaming interface for both sending data to you addon, and returning a data asynchronously to JavaScript.
Publishing your typical Node module to an npm repo is pretty simple - but since your C++ is native executable code, there are some hoops you'll need to jump through so your addon works on all operating systems. In this chapter, we'll cover solutions to common pitfalls involved in publishing/deploying C++ addons.
Addons aren't the only way of moving C++ to the web - in this section we'll take a look at some of the other options, like automation and shared libraries.
One of the most efficient ways to move data between JavaScript and your C++ addons is by using buffers - which get stored outside V8's memory. In this section we'll go through an image processing example that uses buffers to convert pixel data from PNG to BMP.
Save your developers' time by sharing this book with your team. You can purchase a team license that covers up to 50 developers for $99.99, or corporate license if you belong to an even bigger team.
Absolutely not - this book doesn't assume JavaScript or C++ mastery, only working knowledge. In all examples and discussion, advanced topics in JavaScript are only presented when necessary - and always explained.
All C++ code examples are approachable to the C++ novice. Seasoned C++ vets will see where things could be written differently, using more advanced C++, without changing the spirit of the discussion.
My goal is to show you how to integrate - not how clever JavaScript or C++ code can be.
Most programmers learn new topics by scouring the web, piecing together SO posts and API docs to boost their skills.
It normally works pretty well, but once you spend some time researching the V8 C++ API docs, you'll realize that C++ addons are a different animal. Information is scarce, and often refers to out-of-date API's. You find yourself at dead-ends and hitting one roadblock after another.
I went through this pain - and that's why I think this book is worth buying - it will save you from it!
Sign up to for my newsletter on Node.js integration. With your signup, you'll get a free copy of Chapter 2 - Understanding V8 Programming!
I am a Professor of Computer Science at Ramapo College of New Jersey, specializing in computer graphics, and web development. I'm also a consultant and freelance developer, with over ten years of professional experience.
As a professional and as a scholar, I provide innovative solutions in a diverse set of fields - including mechanical engineering, computational biology, environmental science, and organic chemistry.
I teach Web Application Development, Operating Systems, Computer Graphics, Database Systems, and a number of other courses at Ramapo College.