Node.js Flashcards

1
Q

What is a CLI?

A

Command line interface, an interface between users and a command line interpreter/processor. It takes in lines of text as commands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a GUI?

A

Graphical user interface, allows a user to interact with a computer through graphical icons (as opposed to text lines like with a CLI)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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`
A
  • 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
    - The console.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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Node.js?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What can Node.js be used for?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a REPL?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When was Node.js created?

A

Initial release was May 27, 2009

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What back end languages have you heard of?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a JavaScript module?

A

It is a single .js file meant to separate out some functionality (modularized code, modular programming)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What values are passed into a Node.js module’s local scope?

A

module, exports, \_\_dirname , \_\_filename, require

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give two examples of_truly_global variables in a Node.js program.

A
  • global
    • process
    • console
    • some global functions: clearImmediate(), clearInterval(), clearTimeout(), setImmediate/Interval/Timeout
    • There are some global classes as well
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose ofmodule.exportsin a Node.js module?

A

Allow things (any data types) to be returned from a module called by require()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you import functionality into a Node.js module from another Node.js module?

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a directory?

A

A store of “directions” to files or other directories

A file that lists locations to other files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a relative file path?

A

A path to a file that starts/assumes a starting position of the current directory
Set of directions from current directory to there

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an absolute file path?

A

A path to a file that has the entire path explicitly noted

From the root of the file system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What module does Node.js include for manipulating the file system?

A

The fs.js module

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What method is available in the Node.jsfsmodule for writing data to a file?

A

fs.writeFile() although there is also fs.write() which does something similar with file descriptors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Are file operations using thefsmodule synchronous or asynchronous?

A

They are asynchronous (unless we use something like readFileSync(), so sometimes synchronous)

20
Q

What is a client?

A

A requester of services from a server

21
Q

What is a server?

A

A provider of services to one or many clients (does the processing)

22
Q

Which HTTP method does a browser issue to a web server when you visit a URL?

A

GET

23
Q

What is on the first line of an HTTPrequestmessage?

A
  • The type of request (GET, POST, PUT), known as an HTTP method
    • The URL
    • The HTTP version
24
Q

What is on the first line of an HTTPresponsemessage?

A
  • The protocol
    • The status code (200, 404, etc)
    • The human readable status (Success, file not found, etc.)
25
Q

What are HTTP headers?

A
  • 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
26
Q

Is a body required for a valid HTTP message?

A

No, not required

27
Q

What’s the difference between the Web and the Internet

A

The Internet is the hardware that the Web runs on. The Web is the content (software) of HTML documents the like.

28
Q

What is NPM?

A

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
29
Q

What is a package?

A

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

30
Q

How can you create apackage.jsonwithnpm?

A

Use the CLI command npm init --yes

31
Q

What is a dependency and how to you add one to a package?

A
  • 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 the package.json file. Otherwise, include it in the package.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”
32
Q

What happens when you add a dependency to a package withnpm?

A

It automatically adds the dependency into the package.json file

33
Q

How do you addexpressto your package dependencies?

A

npm install express (after doing npm init)

34
Q

What Express application method starts the server and binds it to a networkPORT?

A

app.listen(PORT, [callbackFn])

35
Q

How do you mount a middleware with an Express application?

A

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

36
Q

Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?

A
  • req - the request object
    • res - the response object
    • next() - the next middleware function in the application’s request-response cycle
37
Q

What is a middleware/middleware function?

A
  • 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
38
Q

What is the appropriateContent-Typeheader for HTTP messages that contain JSON in their bodies?

A

application/json

39
Q

What does theexpress.json()middleware do and when would you need it?

A

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

40
Q

What is the significance of an HTTP request’s method?

A

It signifies the intent of the request, and can help a server developer design how a request should be handled

41
Q

What is Webpack?

A
  • Webpack is a tool that, given an entry point JavaScript file, goes through the require and import 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
42
Q

How do you add adevDependencyto a package?

A

After npm install add the option --save-dev

43
Q

What is an NPM script?

A
  • 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)
44
Q

How do you execute Webpack withnpm run?

A

Add a build to the NPM package.json’s scripts section, with the command being just webpack

45
Q

What doesexpress.static()return?

A

A function serveStatic

46
Q

What is the local\_\_dirnamevariable in a Node.js module?

A

The (absolute path) directory name of the module, for us it was /workspaces/c0522-code-solutions/express-static

47
Q

What does thejoin()method of Node’spathmodule do?

A

Joins strings together into a coherent path