Node.js and Google Cloud
Contributed by Google employees.
- Get an overview of Node.js.
- Learn ways to run Node.js apps.
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, conventional 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 to improve CPU utilization by switching between threads when a thread blocks. However, multi-threading introduces 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: checking to see if an event has occurred and, if so, executing 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. 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 you 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 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
There are five options for running Node.js applications on Google Cloud:
- Compute Engine
- Google Kubernetes Engine
- App Engine flexible environment
- App Engine standard environment
- 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 conventional virtual private server (VPS)—you have complete control of the virtual machine. Compute Engine delivers virtual machines running in Google's worldwide data centers and 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
Google Kubernetes Engine is a powerful cluster manager and orchestration system for running your Docker containers. Google 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 Google Kubernetes Engine 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 App Engine standard environment
The Node.js runtime in the App Engine stanard environment runs within containers inside of secure sandboxes and can balance load by automatically scaling. At any time, your application can be running on one or many instances with requests being spread across all of them. Each instance includes a security layer to ensure that instances cannot inadvertently affect each other.
Node.js on Cloud Functions
Cloud Functions is a lightweight, event-based, asynchronous compute system 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.
Read more about Node.js on Google Cloud.