Node.js + NPM + Express Flashcards

1
Q

What is Node.js?

A

a program that allows JavaScript to be run outside of a web browser

commonly used to build back ends for Web applications, command-line programs, or any kind of automation that developers wish to perform

powered by V8– the same JavaScript engine in the Google Chrome browser

its primary focused on creating simple, easy-to-build network clients and servers

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

primarily used for non-blocking, event-driven servers, due to its single-threaded nature

It’s used for web sites and back-end API services

used for outside of the browser

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) is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user

simply type the node command to initiate the REPL session

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 is a computer process?

A

the instance of a computer program that’s being executed by one or many threads (running tasks)

instance of a running program

threading- how the cpu remembers what it was doing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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, so having knowledge of computer processes is important

if were writing front end code and the site is connecting to backend, we have to know how to work with backend (node.js, postgress) as well, thats multiple process

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

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

A

The PROCESS object is a GLOBAL OBJECT that provides information about, and control over, the current Node.js process

As a global, it is always available to Node.js applications without using require()

you can handle command line arguments with the process object using process.argv

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

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

A

just typing process in the terminal/console since process is a global object

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

What is process.argv in Node.js? And what does it return?

A

process.argv returns an array containing the command line arguments passed when the Node.js process was launched

returns an array of strings

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

What is a JavaScript module?

A

in JS, a module is a single .js file

Node.js supports modules using a system heavily influenced by CommonJS

Authors of Node.js programs strive to separate their code into modules that each provide a small chunk of functionality

The program as a whole is the result of all of these modules working together in concert.

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

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

A

exports, require, module, __filename, __dirname

directory and file name of the current module

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

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

A

processed, console

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

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

A

to export the current file its in

allows variables from one file to be used in another file

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

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

A

with required() and passing in a relative path to the file

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

What is the JavaScript Event Loop?

A

event loop’s job is to look at the stack and the task queue, if the stack is empty, it takes the first thing on the queue and pushes onto the stack then runs the code in the stack

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

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

A

blocking executes the code in series. You cant continue until an operation (http request for ex) is complete

In Node.js doc: blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes.

non-blocking refers to code that doesn’t block execution

blocking methods execute synchronously while non-blocking methods execute asynchronously

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

What is a directory?

A

is known as a folder or a collection or files

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

What is a relative file path?

A

a location in the current working directory (or the present working directory/ pwd)

relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory

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

What is an absolute file path?

A

location from the root directory (starts w a /)

all existing directories is in the root directory

in other words, you can say that an absolute path is a complete path from start of actual file system from / directory

20
Q

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

A

fs module

fs module provides an API for interacting with the file system

21
Q

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

A

writeFile method

22
Q

Are file operations using the fs module synchronous or asynchronous?

A

async by default but sync

but it says that fs.writeFileSync returns undefined?

23
Q

What is NPM?

A

its a software registry

developers use NPM to share and borrow packages to solve a particular problem

npm consists of three distinct components (when people talk about NPM, they could be talking about one of 3 things):

1) the website - to search packages
2) the Command Line Interface (CLI) - command line client / NPM client, to run/interact with npm from the registry
3) the registry - public database of information about packages people are sharing

24
Q

What is a package?

A

reusable code

its a directory with one or more files in it that also has a file called file.json (with some meta data about this package)

25
Q

How can you create a package.json with npm?

A

navigate to the root directory of your package and run the command “npm init” with the –yes or -y flag

-y flag when passed to NPM commands tells the generator to use the defaults instead of asking questions

npm init -y will simply generate an empty npm project without going through an interactive process

26
Q

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

A

a dependency is a specification of the packages your project depends on

by running npm install

27
Q

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

A

it adds it to the package.json dependencies

28
Q

How do you add express to your package dependencies?

A

by running command npm install express

29
Q

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

A

listen method

var express = require('express')
var app = express()
app.listen(3000)
30
Q

How do you mount a middleware with an Express application?

A

using the use method of the app object

31
Q

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

A

request and response objects

32
Q

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

A

application/json

33
Q

✧ what is Express?

A

npm package

a minimal and flexible Node.js web app framework

it is a routing and middleware web framework

an Express application is essentially a series of middleware function calls

34
Q

✧ what is a middleware?

A

middleware is a function or program that is going to run between the time that the server gets the request and the time the server sends the request out to the client – middleware is anything that happens between the time the server gets the request and the time the server sends out a response

middleware functions have access to the request object (req), the response object (res), and the next middleware function in the app’s request-response cycle

middleware functions can perform the following tasks:

  • execute any code
  • make changes to the request and the response object
  • end the request-response cycle
  • call the next middleware function in the stack
35
Q

✧ What is routing?

A

determines how an app responds to a client request to a particular endpoint, which is a URL (or path) and a specific HTTP request method (GET, POST, DELETE, PUT, etc)

syntax: app.METHOD(PATH, HANDLER)

36
Q

✧ What is the purpose of res.json()? What arguments does it take?

A

sends a JSON response

this method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify()

the parameter can be any JSON type— object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON

37
Q

✧ What is the use() method of Express application objects?

A

each app.use(middleware) is called every time a request is sent to the server.

app.use() acts as a middleware in express apps. Unlike app.get() and app.post() (& other app.METHOD), you actually can use app.use() without specifying the request URL. In such a case what it does is, it gets executed every time no matter what URL’s been hit

38
Q

✧ What is the method that sends the HTTP response?

A

res.send()

39
Q

✧ What is Route Parameters?

A

Route parameters are named URL segments that are used to capture the values specified at their position in the URL

the captured values are populated in the req.params object

40
Q

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

A

returns a middleware

it gives your app the ability parse json

we need it if we want our program to able to handle client request bodies that require json

41
Q

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

A

returns a middleware

it gives your app the ability parse json

we need it if we want our program to able to handle client requests that have json bodies (when the content type is application/json??)

42
Q

What is Webpack?

A

runs on node.js

a tool that lets you bundle your js applications

called a module bundler

43
Q

How do you add a devDependency to a package?

A

npm install webpack –save-dev

your webpack gets used when its being prepare/ build, not when its running, that why we only need –save-dev

run time vs build time

ex: we run express directly at run time

44
Q

What is an NPM script?

A

key value pair in your package.json, they key is a short name for the value

a shortcut for a command line

45
Q

How do you execute Webpack with npm run?

A

npm run scriptName

ex: npm run build

46
Q

How are ES Modules different from CommonJS modules?

A
  • ES Modules syntax is more compact
  • their structure is flat rather than nested
  • their module bundling is better than CommonJS modules

es module is part of the language now whereas commonjs is a node language

47
Q

What kind of modules can Webpack support?

A

ES Modules and CommonJS modules