Node.js Flashcards

1
Q

What is Node.js?

A

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)

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

What can Node.js be used for?

A

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

What is a REPL?

A

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

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

When was Node.js created?

A

2009

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

What back end languages have you heard of?

A
Python
Java
Ruby
PHP
JS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a computer process?

A

the instance of a program that is being executed by 1+ threads

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

Roughly how many computer processes are running on your host operating system (Task Manager or Activity Monitor)?

A

a lot
400-500 MAC
200ish Windows

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

Why should a full stack Web developer know that computer processes exist?

A

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

What is the process object in a Node.js program?

A

global object that provides information about, and control over, the current Node.js process

doesn’t need “ require() “ to access

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

How do you access the process object in a Node.js program?

A

use variable “process” & dot notation to access properties/methods

process.property

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

What is the data type of process.argv in Node.js?

A

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

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

What is a JavaScript module?

A

a single js file

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

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

A

all of these values will change for each module:

\_\_dirname ---> string
\_\_filename --> string
exports --> object
module --> object
require --> function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give two examples of truly global variables in a Node.js program.

A

clear or set Interval(object)
clear or set Timeout(object)
console
process

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

What is the purpose of module.exports in a Node.js module?

A

export a function to another js file for use

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

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

A
use REQUIRE to load content of file into memory
const add = require( ' ./jsFileName ');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

requires() returns what?

A

module.exports

if it is 1 function, returns the function

if it is object with many functions, will return the object

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

What is the JavaScript Event Loop?

A

responsible for:

  • executing code
  • collecting & processing events
  • executing queued sub-tasks
19
Q

What is different between “blocking” and “non-blocking” with respect to how code is executed?

A

blocking: synchronous - any additional JS code in Node.js process must wait until a operation completes
nonblocking: asynchronous - usually has a callback function

20
Q

What is a directory?

A

a folder on our hard drive that holds files

21
Q

What is a relative file path?

A

internal path in same directory
file path relative to where you’re currently at

(adding “ ./ “ if in current folder)

22
Q

What is an absolute file path?

A

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

23
Q

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

A
fs module (File System)
must [require] it in if fs is needed
24
Q

What method is available in the Node.js fs module for writing data to a file?

A

fs.writeFile

25
Q

Are file operations using the fs module synchronous or asynchronous?

A

asynchronous

26
Q

What is NPM?

A

node package manager

allows us to install useful tools/packages into our projects

27
Q

What is a package?

A

a file or directory that is described by a [ package.json ] file

a reusable bit of code - tools, libraries, etc

28
Q

How can you create a package.json with npm?

A

cd to root directory for your package;
use command:

npm init –yes

29
Q

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

A

package references that are used by your library without which it cannot work

something that your project needs to work

command:
npm install packageName

30
Q

What happens when you add a dependency to a package with npm?

A

npm install packageName => all will install to the node_modules folder

node_modules directory is created within root folder

31
Q

npm install

A

[ npm install ] by itself will read package.json and install all dependencies (if you cloned a project from somewhere)

32
Q

npm new project

A

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)
33
Q

How do you add express to your package dependencies?

A

npm install express

34
Q

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

A

app.listen(port# (usually a variable), callback function)

35
Q

how to require express in js file

A
const express = require('express')
const app = express ();

app.method….

36
Q

servers

A

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

37
Q

How do you mount a middleware with an Express application?

A

app.use

all requests route through that function

38
Q

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

A

app.use(req, res, next)

39
Q

What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?

A

application/json

40
Q

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

A

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)

41
Q

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

A

parses for JSON data and stores it in req.body

need it when we need to store JSON data input

42
Q

What does express.static() return?

A

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

43
Q

What is the local __dirname variable in a Node.js module?

A

string - absolute path to directory the module is in (or where the folder the file is in)

44
Q

What does the join() method of Node’s path module do?

A

takes 2+ file path segments strings and joins them