Welcome, everyone. Today, we're going to talk about Node.js and specifically how to create an HTTP server from scratch. My name is Marco Ippolito. I'm a Senior Developer Experience Engineer at NearForm. I'm also a Node.js core contributor and open-source enthusiast.
So let's get started. What I mean from scratch is literally from scratch. So we will create a native add-on, and we will not use Node.js standard library. So what we will do is call libuv directly and write a bit of C++. But even if you are not a C++ developer, don't be scared because it will be very simple.
So what is a native add-on? A native add-on is a module written in C++ or in C, but it can also be written in Rust that extends some of the low-level functionalities of Node.js, such as libuv. If you don't know what libuv is, we will talk about it later in detail. And also V8, which is the JavaScript engine, Zlib, and many other libraries that compose Node.js. So this native add-on allows us to access the system that we are using, and we could normally not access it in JavaScript.
To create a native add-on, we need a library called Node.addon-api. Node.js has a bunch of APIs that are public and are released. And this package provides the header files to make the development much, much easier. And they are also stable, which means that if you update the Node.js versions, they're not going to break. They're going to be stable. And it's also maintained by the Node.js team. So as you can see, it has quite a few downloads on NPM.
The other library that we need is Node.jyp. So historically, Node.js add-ons have been built with Node.jyp. It was used by Chromium at the beginning, and it was adopted in the early days of Node.js. But you can also use CMake or whatever tool you use for building C++ applications. And with this build system, it will generate a .node file that we can call from JavaScript. And the final one is bindings. This is a very important one because it's a helper module that allows us to import the .node add-on into our JavaScript file, just like you would require or import a JavaScript module. With bindings, you can import a native add-on. So let's see the project structure.
Comments