Node.js Flashcards
What is Node.js?
an open-source, asynchronous JavaScript runtime environment built on Chrome’s V8 JavaScript engine, which executes outside the browser
What can Node.js be used for?
build scalable server side network applications
What is a REPL?
a read-eval-print loop is a language shell
When was Node.js created?
2009
What back end languages have you heard of?
PHP, Python, Ruby, Java, C#
What is a CLI?
command line interface, a text-based user interface
What is a GUI?
graphical user interface
Give at least one use case for each of the commands listed in this exercise.
man -read about a command in the manual
cat -combine text files, print out
ls -list contents of a directory
pwd -view current directory
echo -write a string to a text file, command line
touch -create a new text file, updated access stamp on files
mkdir -create a new directory
mv -move a file to another directory, rename files
rm -remove a file from the hard drive
cp -copy a file
What are the three virtues of a great programmer?
laziness, impatience, hubris
What is a computer process?
the instance of a computer program that is being executed by one or more threads
Roughly how many computer processes are running on your host operating system (Task Manager or Activity Monitor)?
~200
Why should a full stack Web developer know that computer processes exist?
a full stack web app is made form multiple processes together
What is the process object in a Node.js program?
a global that provides information about, and control over, the current Node.js process
How do you access the process object in a Node.js program?
access it directly with ‘process’ in Node.js, since it is a global object
What is the data type of process.argv in Node.js?
an array of strings
What is a JavaScript module?
a single JavaScript file
What values are passed into a Node.js module’s local scope?
parameters: exports, require, module, __filename, __dirname
Give two examples of truly global variables in a Node.js program.
clearImmediate(immediateObject)
console
process
What is the purpose of module.exports in a Node.js module?
to export code to be used in another module
How do you import functionality into a Node.js module from another Node.js module?
by calling the require() function (with the argument being a string of the path to the JavaScript file) and assigning the return to a variable
What is the JavaScript Event Loop?
a cycling process for how JavaScript executes code in a non-blocking manner, checks if there is something in the call stack again and again, and if not then checks the callback queue (which is made up of completed web APIs),
-if there is something in the queue, it gets moved to the call stack
What is different between “blocking” and “non-blocking” with respect to how code is executed?
blocking code waits for each line of code to be resolved in the call stack, while non-blocking code moves code off to a task queue so that the call stack is not blocked from continuing to run
What is a directory?
a pointer or container for files
What is a relative file path?
the location of a file from the current file
What is an absolute file path?
a complete path, the location of a file from anywhere
starts with a slash /
What module does Node.js include for manipulating the file system?
fs module
What method is available in the Node.js fs module for writing data to a file?
the fs.writeFile() method
Are file operations using the fs module synchronous or asynchronous?
asychronous on fs module, though both are possible in node
stdin
process input
stdout
process output for normal output (logs to the terminal), ex. process.stdout.write()
stderr
process output for errors
TCP
machines connect to a network, then can connect to each other, sending data back and forth
What is a client?
a program or computer which is a data requestor or accesses a service, or a web browser
What is a server?
a program or computer which is a data provider or provides functionality to clients
Which HTTP method does a browser issue to a web server when you visit a URL?
GET method
What is on the first line of an HTTP request message?
HTTP method, request target, HTTP version
What is on the first line of an HTTP response message?
protocol version, status code, status text
What are HTTP headers?
additional information which specifies the request or describe the body included in the message
Is a body required for a valid HTTP message?
No, it’s optional
What is NPM?
a JavaScript package manager, CLI, website, software registry
What is a package?
a file in a directory with a package.json, reusable code which can be shared
How can you create a package.json with npm?
the CLI command “npm init –yes”
What is a dependency and how do you add one to a package?
- a package that your app relies on to function
- npm install
What happens when you add a dependency to a package with npm?
the package and any of its dependencies are installed in the ‘node-modules’ folder, also altering the package.json file
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[,host[,backlog]]][,callback])
What is Express?
back end web application framework for Node.js
How do you mount a middleware with an Express application?
app.use([path,] callback [,callback…]) method
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
req (request) and res (response) objects
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?
HTTP request methods mean what we want to do, but can actually do anything
What does the express.json() middleware do and when would you need it?
- parses the JSON request body to a JavaScript object
- when you will receive JSON from the request body
npm run dev:server
- nodemon watches js so it auto restarts server
Promise
an object representing the eventual completion or failure of asynchronous operation
What are the three states a Promise can be in?
pending, fulfilled, rejected
How do you handle the fulfillment of a Promise?
.then() method, pass a handler function as an argument
How do you handle the rejection of a Promise?
.then() or .catch() method, pass a handler function as an argument