This tutorial shows how to prepare your computer for Node.js development. Follow this tutorial to install Node.js and relevant tools.
- Get an overview of Node.js
- Learn ways to run Node.js app on Google Cloud Platform
What is Node.js?
Event-driven programming is the idea that events control of the flow of a program. When an event occurs, code registered to handle the event is executed. When performing I/O, traditional programming is commonly synchronous, meaning that an executing thread must wait for the I/O operation to complete before it can continue its execution. Solutions such as multi-threading were devised in order to improve CPU utilization by switching between threads when a thread blocks. Multi-threading does however introduce the overhead of switching between threads and remembering each thread’s context.
At the heart of Node.js is an event loop that performs two tasks: Check to see if an event has occurred and if so, execute the registered event handler. This allows Node.js to maintain a single thread that is constantly working, executing event handlers as events occur.
Node.js was originally written to handle text—specifically HTML. The
Buffer class was introduced to allow Node.js to handle binary data.
therefore immune to some of the normal effects of garbage collection. Buffers
can be sliced and copied. The individual bytes of a Buffer can be manipulated.
Buffers can be created from Strings of various encodings, and can be converted
into Strings of various encodings. Buffers give Node.js a range of capabilities
that increase the usefulness of Node.js.
Node.js Module System
Node.js does away with the global namespace and implements the CommonJS
standard (though ES2015 modules are on the way). Files and modules have a
one-to-one relationship. Each file chooses exactly what will be publicly exposed
by the file’s module when it is loaded by other modules. Node.js looks for core
modules first, then modules with relative path names, and finally looks in
node_modules when searching for modules. Each module is cached the first
time it is loaded, so the contents of each module are only initialized once.
Node.js Core Library and Ecosystem
The Node.js core library provides the developer with a number of tools for performing I/O, working with HTTP, files, the OS, streams, and much more. In addition, npm is a large repository of community and open source packages for Node.js. Node.js is at home in small, rapidly developing products, probably because Node.js and its ecosystem are still (relatively) small and under rapid development. Node.js is emerging in the enterprise as the ecosystem grows, as the runtime matures, and as it gains improved robustness and debugging capabilities.
Running Node.js Apps on Google Cloud Platform
There are four options for running Node.js applications on Google Cloud Platform:
- Google Compute Engine
- Google Kubernetes Engine
- Google App Engine flexible environment
- Google Cloud Functions
Node.js on Compute Engine
Hosting a Node.js application on Compute Engine is just like hosting a Node.js application on a traditional Virtual Private Server (VPS)—you have complete control of the virtual machine. Google Compute Engine delivers virtual machines running in Google's innovative data centers and worldwide fiber network. Compute Engine's tooling and workflow support enable scaling from single instances to global, load-balanced cloud computing.
Node.js on Kubernetes Engine
Kubernetes Engine is a powerful cluster manager and orchestration system for running your Docker containers. Kubernetes Engine schedules your containers into the cluster and manages them automatically based on requirements you define (such as CPU and memory). It's built on the open source Kubernetes system, giving you the flexibility to take advantage of on-premises, hybrid, or public cloud infrastructure. With it you can orchestrate containerized Node.js deployments using Kubernetes' container management infrastructure.
Node.js on App Engine flexible environment
App Engine flexible environment is a fully managed, Docker-based Platform-as-a-Service (PaaS). You can deploy, monitor, and scale Node.js applications without having to know anything about virtual machines, containers, or compute infrastructure. Google manages your Node.js application for you.
Node.js on Cloud Functions
Cloud Functions is a lightweight, event-based, asynchronous compute solution that allows you to create small, single-purpose functions that respond to cloud events without the need to manage a server or a runtime environment. You deploy individual functions written in Node.js that are executed in response to various cloud events, including direct HTTP requests.