What is Node?
Node.js, otherwise known as Node, is an open source run-time environment that allows you to execute JavaScript code outside of a browser. Node can be used similarly to, for example, a Python interpreter that runs Python code. It’s available cross-platform, meaning you can host the Node.js application in all major operating systems including Windows, Mac OS and Linux.
“Node is a single-threaded, single-process system which enforces shared-nothing design with OS process boundaries. It has rather good libraries for networking. I believe this to be a basis for designing very large distributed programs. The “nodes” need to be organized: given a communication protocol, told how to connect to each other. In the next couple months [Node’s development team] is working on libraries for Node that allow these networks.”
Before Node, JavaScript could only be run inside a browser and thus had no access to the file system – one of the reasons there is no print statement in JS. Node is built off the V8, the Chrome JavaScript engine, but made into an independent runtime environment. It allowed JS to read and write files and talk to the internet independently. This created the option to make web servers with Node.js and it is now a popular choice for building full-stack applications (i.e, software development including both front-end and back-end).
Why Node?
Write some stuff here.
Asynchronous & Non-blocking
Synchronous programming models and languages such as PHP require things to happen one at a time. This means each process/ line of code waits for a return from the line ahead of it before executing. Compared to that, an asynchronous language such as Node allows the handling of multiple requests simultaneously.
Example: a frequent task for web servers is to open a file on the server and return the content to the client. In environments such as PHP or Python, the entire thread (a sequence of instructions to execute) is blocked and a new one must be created for the operation. This executes a sequence of instructions looking somewhat like:
- Task is sent to computers file system
- Waits for file system to open and read file
- Returns result to client
- Open to next request
Meanwhile, Node.js is what is known as a single-threaded process; it uses only a single thread for handling requests and a main worker thread. When the request is made, Node.js sends that request to the requests thread and returns to the main worker thread. It may receive another request before completion of the previous one – in that case it simply sends the request to the same thread and returns to work. Once a response is given, Node returns it to the client. How Node handles the request:
- Task is sent to computers file system
- Continues other tasks and open to next request
- When file system has open and read the file, returns it to the client
This is an example of a non-blocking model. In such models, the program doesn’t wait for operations to return a result before moving on; not blocking the execution of further operations.
Node’s single-threaded non-blocking I/O model nearly eliminates wait times between requests and increases system efficiency.
What can Node do?
Functions
- Node.js can build dynamic web pages (displaying different content based on user behavior while retaining layout & design for all users) – this will be expanded on later in the course
- Node.js can create, read, write, delete, and modify files on the server using the fs module
fs.writeFile('<fileName>',<contenet>, callbackFunction)
fs.appendFile('<fileName>',<contenet>, callbackFunction)
fs.open('<fileName>',<file_open_mode>, callbackFunction)
- Node.js can read HTML form data
- Node.js can add, delete, modify data in your database
Scalable Network Applications
Node is designed to build scalable network applications, and it is also commonly used for real-time applications (RTAs). Its asynchronous, event-driven model allows it to handle heavy traffic on I/O operations. Such applications include:
- streaming services – Netflix moved half of its API to Node.js in 2018
- browser games – tools such as
EventEmitter
allow for easier management for games. Combined with HTML5 and Socket.IO, Node can create browser games without need for third-party plugins
- Chat applications – Nodes Socket.IO library enables real-time, bidirectional, and event-based communication between the browser and the server, greatly increasing the ease at which group-chat applications are built
- Many more!
Due to its asynchronous model and ability compiles JavaScript directly to native machine code before executing it
Next Section
2.2 Modules and PackagesCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.