Node.js Flashcards
What is a CLI?
Command line interface, an interface between users and a command line interpreter/processor. It takes in lines of text as commands
What is a GUI?
Graphical user interface, allows a user to interact with a computer through graphical icons (as opposed to text lines like with a CLI)
Give at least one use case for each of the commands listed in this exercise.
- `man` - `cat` - `ls` - `pwd` - `echo` - `touch` - `mkdir` - `mv` - `rm` - `cp`
-
man
- Anytime you want to know about a command, pull up the manual
-
cat
- View the contents of a file
- Put the contents of multiple files into one file
-
ls
- Show the contents of a directory, either the current directory or otherwise -
pwd
- Know where you are, or output where you are to a file -
echo
- Theconsole.log
of the command line, prints something
- Quickly make a file with one line of text in it -
touch
- Make an empty file -
mkdir
- Make a directory, or directory within a directory -
mv
- Renaming a file
- moving a file -
rm
- Delete files or directories -
cp
- Make copies of files
What is Node.js?
A JavaScript runtime that is asynchronous and event driven
A runtime is “where the code gets run” and has all the stuff that handles interacting with memory, garbage collecting, interfacing with the OS, how parameters get passed, etc.
What can Node.js be used for?
They claim it is “to build scalable network applications”
JavaScript can be used to write command line tools and server-side scripting
It allows for JavaScript to be the language for both server and client side
What is a REPL?
read-eval-print-loop
Evaluates code one line at a time rather than running an entire file
Takes in user input one line at a time (read), evaluates the code (eval), and outputs the result (print), and it repeats (loop)
~Interactive shell environment
When was Node.js created?
Initial release was May 27, 2009
What back end languages have you heard of?
Scripting/interpreted: Python, JavaScript, Ruby, PHP, Perl, SQL (can’t really write a backend with this)
Compiled: C, C++, Go, Haskell, Crystal, Rust
Partially compiled: Java (JVM), Scala (JVM), clojure (JVM), kotlin (JVM), C# (.NET CLR), F# (.NET CLR), Visual Basic
What is a JavaScript module?
It is a single .js
file meant to separate out some functionality (modularized code, modular programming)
What values are passed into a Node.js module’s local scope?
module
, exports
, \_\_dirname
, \_\_filename
, require
Give two examples of_truly_global variables in a Node.js program.
-
global
process
console
- some global functions:
clearImmediate()
,clearInterval()
,clearTimeout()
,setImmediate/Interval/Timeout
- There are some global classes as well
What is the purpose ofmodule.exports
in a Node.js module?
Allow things (any data types) to be returned from a module called by require()
How do you import functionality into a Node.js module from another Node.js module?
The source module should have the desired functionality in its `module.exports` The calling module should then `require()` the source file - The return of that `require` call will be `module.exports` from the source, to then be handled however desired (likely assigned to a variable)
What is a directory?
A store of “directions” to files or other directories
A file that lists locations to other files
What is a relative file path?
A path to a file that starts/assumes a starting position of the current directory
Set of directions from current directory to there
What is an absolute file path?
A path to a file that has the entire path explicitly noted
From the root of the file system
What module does Node.js include for manipulating the file system?
The fs.js
module
What method is available in the Node.jsfs
module for writing data to a file?
fs.writeFile()
although there is also fs.write()
which does something similar with file descriptors
Are file operations using thefs
module synchronous or asynchronous?
They are asynchronous (unless we use something like readFileSync()
, so sometimes synchronous)
What is a client?
A requester of services from a server
What is a server?
A provider of services to one or many clients (does the processing)
Which HTTP method does a browser issue to a web server when you visit a URL?
GET
What is on the first line of an HTTPrequestmessage?
- The type of request (
GET
,POST
,PUT
), known as an HTTP method- The URL
- The HTTP version
What is on the first line of an HTTPresponsemessage?
- The protocol
- The status code (200, 404, etc)
- The human readable status (Success, file not found, etc.)
What are HTTP headers?
- They are generally a space for clients/servers to pass additional information about a request to each other
- Other than the first line (set format) and the body (data and not metadata), there is only the header section left
Is a body required for a valid HTTP message?
No, not required
What’s the difference between the Web and the Internet
The Internet is the hardware that the Web runs on. The Web is the content (software) of HTML documents the like.
What is NPM?
Formerly known as Node Package Manager, npm can refer to one of three things
- The NPM website that allows setup and configuration - The NPM CLI, which is how developers will usually interface with NPM - The NPM registry, which is the database with all the packages/modules that NPM has available
What is a package?
A package consists of the file(s) (.js
files) that offer the desired functionality as well as a package.json
file that details some metadata regarding the other files
A package is a file or directory described by a package.json
file
How can you create apackage.json
withnpm
?
Use the CLI command npm init --yes
What is a dependency and how to you add one to a package?
- A dependency is a package from an external source that is used in your program, making your program dependent on it
- The
npm install \_\_\_
command automatically adds the dependency to thepackage.json
file. Otherwise, include it in thepackage.json
file. - When you download a package, npm will check the
package.json
of that package and download those dependencies you don’t have (and so on). These are “transitive dependencies”
- The
What happens when you add a dependency to a package withnpm
?
It automatically adds the dependency into the package.json
file
How do you addexpress
to your package dependencies?
npm install express
(after doing npm init
)
What Express application method starts the server and binds it to a networkPORT
?
app.listen(PORT, [callbackFn])
How do you mount a middleware with an Express application?
For application-level middleware, mount it by using the app
object methods (.use
and .METHOD
for example)
- Without any middleware, all you get are 404s
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
-
req
- the request object-
res
- the response object -
next()
- the next middleware function in the application’s request-response cycle
-
What is a middleware/middleware function?
- Functions that have access to the request and response objects as well as the next middleware function in the request-response loop of an application. They can do the following:
- Execute any code
- Make changes to the request and response objects
- End the request-response cycle
- Call the next middleware function in the stack
- If it does not end the request-response cycle, it must call the next middleware function (otherwise the request will be left hanging)- Similar to event listeners, but not 100% aligned
What is the appropriateContent-Type
header for HTTP messages that contain JSON in their bodies?
application/json
What does theexpress.json()
middleware do and when would you need it?
Allows for parsing of JSON requests on requests made to an endpoint (route)
You need it when you are expecting to receive JSON requests and want to work with the JSON data as a JavaScript object
What is the significance of an HTTP request’s method?
It signifies the intent of the request, and can help a server developer design how a request should be handled
What is Webpack?
- Webpack is a tool that, given an entry point JavaScript file, goes through the
require
andimport
statements to find the required JavaScript code and puts them all into one big JavaScript file. It also works with other types of code depending on the loaders used- Module bundler is a thing that does this
How do you add adevDependency
to a package?
After npm install
add the option --save-dev
What is an NPM script?
- They are scripts made available in the
package.json
file that allow you to run commands from NPM (named scripts that can be referenced through NPM)- You can use the usual CLI commands like
echo
but you can also do things provided by NPM (either NPM’s defaults or from the things you install via NPM)
- You can use the usual CLI commands like
How do you execute Webpack withnpm run
?
Add a build
to the NPM package.json
’s scripts
section, with the command being just webpack
What doesexpress.static()
return?
A function serveStatic
What is the local\_\_dirname
variable in a Node.js module?
The (absolute path) directory name of the module, for us it was /workspaces/c0522-code-solutions/express-static
What does thejoin()
method of Node’spath
module do?
Joins strings together into a coherent path