Node.js Flashcards
What is Node.js?
short version: asynchronous server environment executing JS outside browser
long version: asynchronous event-driven open source server environment that executes JavaScript code outside a web browser
powered by V8 (same JS engine as Chrome browser)
What can Node.js be used for?
build back ends for:
- Web applications
- command-line programs
- or any kind of automation that developers wish to perform
Miscellaneous:
- generate dynamic page content
- create, open, read, write, delete, and close files on the server
- collect form data
- add, delete, modify data in your database
What is a REPL?
Read-Eval-Print-Loop (REPL)
interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user
executed piecewise
facilitate exploratory programming and debugging because the programmer can inspect the printed result before deciding what expression to provide for the next read
When was Node.js created?
2009
What back end languages have you heard of?
Python Java Ruby PHP JS
What is a computer process?
the instance of a program that is being executed by 1+ threads
Roughly how many computer processes are running on your host operating system (Task Manager or Activity Monitor)?
a lot
400-500 MAC
200ish Windows
Why should a full stack Web developer know that computer processes exist?
Full stack Web development is based on making multiple processes work together to form one application.
This will be extremely important when learning about applications made of multiple components:
- clients
- servers
- databases
What is the process object in a Node.js program?
global object that provides information about, and control over, the current Node.js process
doesn’t need “ require() “ to access
How do you access the process object in a Node.js program?
use variable “process” & dot notation to access properties/methods
process.property
What is the data type of process.argv in Node.js?
returns ARRAY containing command line args passed when node launched
argv[0] = process.execpath (useless to us) argv[1] = path to js file being executed (useless to us) argv[2+] = addtl command line args passed (USEFUL!)
therefore always start at argv[2] bc thats where actual input starts
What is a JavaScript module?
a single js file
What values are passed into a Node.js module’s local scope?
all of these values will change for each module:
\_\_dirname ---> string \_\_filename --> string exports --> object module --> object require --> function
Give two examples of truly global variables in a Node.js program.
clear or set Interval(object)
clear or set Timeout(object)
console
process
What is the purpose of module.exports in a Node.js module?
export a function to another js file for use
How do you import functionality into a Node.js module from another Node.js module?
use REQUIRE to load content of file into memory const add = require( ' ./jsFileName ');
requires() returns what?
module.exports
if it is 1 function, returns the function
if it is object with many functions, will return the object
What is the JavaScript Event Loop?
responsible for:
- executing code
- collecting & processing events
- executing queued sub-tasks
What is different between “blocking” and “non-blocking” with respect to how code is executed?
blocking: synchronous - any additional JS code in Node.js process must wait until a operation completes
nonblocking: asynchronous - usually has a callback function
What is a directory?
a folder on our hard drive that holds files
What is a relative file path?
internal path in same directory
file path relative to where you’re currently at
(adding “ ./ “ if in current folder)
What is an absolute file path?
the FULL file path to get to a specific file/folder
path from root directory (beginning of hard drive) and every folder it takes to get to the specific file
NOTE: never hardcode an absolute path; it runs on your machine but may not on others due to different file structures
What module does Node.js include for manipulating the file system?
fs module (File System) must [require] it in if fs is needed
What method is available in the Node.js fs module for writing data to a file?
fs.writeFile
Are file operations using the fs module synchronous or asynchronous?
asynchronous
What is NPM?
node package manager
allows us to install useful tools/packages into our projects
What is a package?
a file or directory that is described by a [ package.json ] file
a reusable bit of code - tools, libraries, etc
How can you create a package.json with npm?
cd to root directory for your package;
use command:
npm init –yes
What is a dependency and how to you add one to a package?
package references that are used by your library without which it cannot work
something that your project needs to work
command:
npm install packageName
What happens when you add a dependency to a package with npm?
npm install packageName => all will install to the node_modules folder
node_modules directory is created within root folder
npm install
[ npm install ] by itself will read package.json and install all dependencies (if you cloned a project from somewhere)
npm new project
npm init -y (to install package.json)
npm install package-name (install the rest of the packages)
if installed wrong package: npm uninstall package-name (will delete all files from node_module and remove it from dependency list in package.json)
How do you add express to your package dependencies?
npm install express
What Express application method starts the server and binds it to a network PORT?
app.listen(port# (usually a variable), callback function)
how to require express in js file
const express = require('express') const app = express ();
app.method….
servers
errors show up where you ran the server
(node filename.js)
must stop server with ctrl + C
app.listen should be the last line in the file
[ http POST :3000/path ] is a shortcut
How do you mount a middleware with an Express application?
app.use
all requests route through that function
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
app.use(req, res, next)
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
What is the significance of an HTTP request’s method?
None on its own; Developers give the request significance
the request method doesn’t do anything unless developer defines what to do (ex/ delete won’t delete anything unless code tells it to)
What does the express.json() middleware do and when would you need it?
parses for JSON data and stores it in req.body
need it when we need to store JSON data input
What does express.static() return?
returns a function (function checks for a file to see if it exists, then returns the file)
or HTTP 404 if files not found
sole purpose is to serve files from static folder
What is the local __dirname variable in a Node.js module?
string - absolute path to directory the module is in (or where the folder the file is in)
What does the join() method of Node’s path module do?
takes 2+ file path segments strings and joins them