Key Concepts

Review core concepts you need to learn to master this subject

Node.js REPL

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Node.js comes with REPL, an abbreviation for read–eval–print loop. REPL contains three different states:

a read state where it reads the input from a user, the eval state where it evaluates the user’s input *the print state where it prints out the evaluation to the console.

After these states are finished REPL loops through these states repeatedly. REPL is useful as it gives back immediate feedback which can be used to perform calculations and develop code.

Node.js Global Object

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

The Node.js environment has a global object that contains every Node-specific global property. The global object can be accessed by either typing in console.log(global) or global in the terminal after RPL is running. In order to see just the keys Object.keys(global) can be used. Since global is an object, new properties can be assigned to it via global.name_of_property = 'value_of_property'.

Node.js Process Object

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

A process is the instance of a computer program that is being executed. Node has a global process object with useful properties. One of these properties is NODE_ENV which can be used in an if/else statement to perform different tasks depending on if the application is in the production or development phase.

Node.js process.argv

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

process.argv is a property that holds an array of command-line values provided when the current process was initiated. The first element in the array is the absolute path to the Node, followed by the path to the file that’s running and finally any command-line arguments provided when the process was initiated.

Node.js process.memoryUsage()

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

process.memoryUsage() is a method that can be used to return information on the CPU demands of the current process. Heap can refer to a specific data structure or to the computer memory.

Node.js Modules

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

In Node.js files are called modules. Modularity is a technique where one program has distinct parts each providing a single piece of the overall functionality - like pieces of a puzzle coming together to complete a picture. require() is a function used to bring one module into another.

Node.js Core Modules

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Node has several modules included within the environment to efficiently perform common tasks. These are known as the core modules. The core modules are defined within Node.js’s source and are located in the lib/ folder. A core module can be accessed by passing a string with the name of the module into the require() function.

Node.js Local Modules

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

In Node.js files are considered modules. Modules that are created locally are called local modules. These local modules are held in an object called module. This object has a property called export which allows a module to be accessed in a different module.

Node Package Manager

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

NPM stands for node-package-manager. An NPM is essentially a collection of code from other developers that we can use. When Node is installed the npm command-line tool is downloaded as well. This command-line tool enables us to interact with the registry via our terminal.

EventEmitter Class

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Node.js has an EventEmitter class which can be accessed by requiring the events core module. Each event emitter instance has an .on() method which assigns a listener callback function to a named event. EventEmitter also has an .emit() method which announces a named event that has occurred.

Asynchronous Node.js

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Node.js is a non-blocking, asynchronous environment. The event loop in Node.js enables asynchronous actions to be handled in a non-blocking way. Node.js provides APIs which allow operations to be put in a queue, waiting to be executed after the previous operation finishes. If synchronous tasks never end, operations waiting in the event-queue will never execute.

Asynchronous Error Handling

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

The Node environment has all the standard JavaScript errors as well as the JavaScript Error class for creating new error instances. Many asynchronous Node APIs use error-first callback functions: callback functions which have an error as the first expected argument and the data as the second argument. If the asynchronous task results in an error, it will be passed in as the first argument to the callback function. If no error was thrown, the first argument will be undefined.

Node.js Input/Output

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Input is data that is given to the computer, while output is any data or feedback that a computer provides. In Node, we can get input from a user by using the stdin.on() method on the process object. We are able to use this because .on() is an instance of EventEmitter. To give an output we can use the .stdout.write() method on the process object as well. This is because console.log() is a thin wrapper on .stdout.write().

Filesystem

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

A filesystem is how you access and organize all data on a computer. The Node.js fs core module is an API for interacting with the file system. Each method available through the fs module has a synchronous version and an asynchronous version. One of the methods in the fs core module is the .readfile() method which allows us to read data from a file.

Web Server

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

Node was designed with back end development needs as a top priority. One of these needs is the ability to create web servers. A web server is a computer process that listens for requests from clients and returns responses. A Node core module designed to meet these needs is the http module. This module has functions that simplify receiving and responding to requests.

Creating A Server

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

http.createServer() is a method that returns an instance of an http.server. The method .listen() in http.server tells the server to “listen” for incoming connections. We give http.createServer() a callback function also known as the requestListener, which will be triggered once the server is listening and receives a request. The requestlistener requests a request object and a response object.

Readable/Writable Streams

//node is typed in the console to access REPL $ node //the > indicates that REPL is running // anything written after > will be evaluated > console.log("HI") // REPL has evaluated the line and has printed out HI HI

In most cases, data isn’t processed all at once but rather piece by piece. This is what we call streams. Streaming data is preferred as it doesn’t require tons of RAM and doesn’t need to have all the data on hand to begin processing it. To read files line-by-line, we can use the .createInterface() method from the readline core module. We can write to streams by using the .createWriteStream() method.

Arrow Chevron Left Icon
What is the Back-end?
Lesson 1 of 2
Arrow Chevron Right Icon
  1. 1
    In this lesson, we’ll explain what makes up the back-end of a web application or website. The back-end can feel very abstract, but it becomes clearer when we explain it in terms of the front-end! T…
  2. 2
    We talked about how the front-end consists of the information sent to a client so that a user can see and interact with a website, but where does the information come from? The answer is a _web ser…
  3. 3
    When a user navigates to google.com, their request specifies the URL but not the filename for today’s Google Doodle . The web application’s back-end will need to hold the logic for deciding which …
  4. 4
    You’ve probably heard that data is a big deal. By some measures, 90% of the world’s data has been generated in just the past two years! From a stored credit card number on an e-commerce site to the…
  5. 5
    When a user navigates to a specific item for sale on an e-commerce site, the price listed for that item is stored in a database, and when they purchase it, the database will need to be updated with…
  6. 6
    Two other concepts we’ll want our server-side logic to handle are authentication and authorization. Authentication is the process of validating the identity of a user. One technique for aut…
  7. 7
    Unlike the front-end, which must be built using HTML, CSS, and JavaScript, there’s a lot of flexibility in which technologies can be used in order to create the back-end of a web application. Devel…
  8. 8
    In order to deliver the front-end of a website or web application to a user, a lot needs to happen behind the scenes on the back-end! Understanding what makes up the back-end can be overwhelming be…
  1. 1
    For a long time, the browser was the only place JavaScript code could be executed. Web developers had to use a different programming language on the front-end than the back-end. It also meant that,…
  2. 2
    REPL is an abbreviation for read–eval–print loop. It’s a program that loops, or repeatedly cycles, through three different states: a read state where the program reads…
  3. 3
    Node was designed with server-side web development in mind and has a lot of thoughtful functionality towards that end. At its most simple, however, it provides the ability to run JavaScript program…
  4. 4
    In computer science, a process is the instance of a computer program that is being executed. You can open Task Manager if you’re on a Windows machine or Activity Monitor from a Mac to see informa…
  5. 5
    Modularity is a software design technique where one program has distinct parts each providing a single piece of the overall functionality. These separate modules come together to build a cohesi…
  6. 6
    In addition to local modules and core modules, we can take advantage of third-party modules. Using libraries created by other developers is an essential aspect of production; we don’t have to reinv…
  7. 7
    Node is often described as having event-driven architecture. Let’s explore what that means. In traditional imperative programming, we give the computer a series of instructions to execute in a pr…
  8. 8
    In server-side development, we often perform time-consuming tasks such as reading files or querying a database. Instead of halting the execution of our code to await these operations or using multi…
  9. 9
    If you’ve worked with JavaScript before, you’re likely familiar with the concept of input/output even if you haven’t heard it called that. At its most abstract, output is any data or feedback tha…
  10. 10
    The Node environment has all the standard JavaScript errors such as EvalError, SyntaxError, RangeError, ReferenceError, TypeError, and URIError as well as the JavaScript Error class for creating ne…
  11. 11
    All of the data on a computer is organized and accessed through a filesystem. When running JavaScript code on a browser, it’s important for a script to have only limited access to a user’s filesy…
  12. 12
    In the previous exercise, we practiced reading the contents of entire files into our JavaScript programs. In more realistic scenarios, data isn’t processed all at once but rather sequentially, piec…
  13. 13
    In the previous exercise, we were reading data from a stream, but we can also write to streams! We can create a writeable stream to a file using the fs.createWriteStream() method: const fs = requi…
  14. 14
    Node was designed with back end development needs as a top priority. One of these needs is the ability to create web servers, computer processes that listen for requests from clients and return res…
  15. 15
    Awesome work! You’ve learned a lot about Node.js including: + Node.js is a JavaScript runtime, an environment that allows us to execute our JavaScript code by converting it into something a compu…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo