Node.js Flashcards

1
Q

What can Node.js be used for?

A

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

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

What is a REPL?

A

A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise.

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

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

A

Global variable. The process object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process is one of them.

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

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

A

The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. // The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.

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

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

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

What is a JavaScript module?

A

A module in JavaScript is just a file containing related code. I
n JavaScript, we use the import and export keywords to share and receive functionalities respectively across different modules. The export keyword is used to make a variable, function, class or object accessible to other modules.

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

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

A

The five parameters — exports , require , module , __filename , __dirname are available inside each module in Node. Though these parameters are global to the code within a module yet they are local to the module (because of the function wrapper as explained above).

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

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

A

Blocking code occupied a call stack, and executed synchronously while non-blocking code execute asynchronously.

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

What is the JavaScript Event Loop?

A

JavaScript has a runtime model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model is quite different from models in other languages like C and Java.

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

What is an absolute file path?

A

path which includes root of the file. starts with a ‘/’

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

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

A

fs.writeFile(file, data[, options], callback)

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

What is NPM?

A

npm is the default package manager for the JavaScript runtime environment Node.js.

It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

/* npm consists of three distinct components:
the website
the Command Line Interface (CLI)
the registry */

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

What is a package (module)?

A

It is a directory with files in it => package.json and some reusable code

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

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

A

A dependency is another package that your package needs in order to work. Dependencies are specified in your pubspec.

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

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

A

A dependency is another package that your package needs in order to work. Dependencies are specified in your pubspec.

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

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

A

The app. listen() function is used to bind and listen the connections on the specified host and port.

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

Network interface

A

Network interface is a piece of hardware on a computer that allows network connectivity (internet) evamples wifi card / adapter Lan card or adapter
- each network interface has own its own IP address
LAN: Local Area Network
Every network interface provides 2^16 “ports”
- the default (aka “well-known”) HTTP port is actually 80
- the defalt (aka “well-known”) Https port is actually 443
- any port number below 1024 requires “admin” privileges to use

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

What is the difference between library and framework ?

A

A library is a code someone wrote (or separate part of your codebase), a library should be reusable. A framework is a specific kind of library. A framework is different from library when is has “inversion of control”
Who calls who? A framework calls YOUR code, a library is called by your code. A framework describes WHEN to call your functions (you don’t cal them).

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

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

Execute any code.
Make changes to the request and the response objects.
End the request-response cycle.
Call the next middleware function in the stack.

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

req (short for “request”)

A

a data model of the HTTP request massage (from the client)

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

res (short for “response”)

A

a data model of the HTTP request to the client

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

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

A

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.

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

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

A

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Although they can also be nouns, these request methods are sometimes referred to as HTTP verbs.

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

Four kinds of http requests

A

GET - read
DELETE - dalete
POST - put
PUT - update

25
Q
A

A relational database is a collection of data items with pre-defined relationships between them. These items are organized as a set of tables with columns and rows. Tables are used to hold information about the objects to be represented in the database.

//A relational database is a type of database that stores and provides access to data points that are related to one another.

26
Q

What is one way to see if PostgreSQL is running?

A

Check the status of the postgresql service. You can do this anytime you want to see if Postgres is running.
“sudo service postgresql status”

27
Q

What is PostgreSQL and what are some alternative relational databases?

A

PostgreSQL is a powerful, free, open source Relational Database Management System (RDBMS). It is often cited as the most advanced open source database of its kind.

Other popular relational databases include MySQL (also free), SQL Server by Microsoft, and Oracle by Oracle Corporation.

28
Q

Top Advantages of Relational Database

A
  • Simple Model. A Relational Database system is the most simple model, as it does not require any complex structuring or querying processes.
  • Data Accuracy. In the relational database system, there can be multiple tables related to one another with the use of a primary key and foreign key concepts. This makes the data to be non-repetitive. There is no chance for duplication of data. Hence the accuracy of data in the relational database is more than any other database system.
  • Easy Access to Data. In the Relational Database System, there is no pattern or pathway for accessing the data, as to another type of databases can be accessed only by navigating through a tree or a hierarchical model. Anyone who accesses the data can query any table in the relational database. Using join queries and conditional statements one can combine all or any number of related tables in order to fetch the required data.
  • Data Integrity. A quality of many relational databases is that they support good guarantees about data integrity. They can store and modify data in a way that makes data corruption as unlikely as possible.
  • Flexibility.
  • Normalization.
  • High Security.
  • Feasible for Future Modifications.
29
Q

What is a database schema?

A

A database schema defines how data is organized within a relational database; this is inclusive of logical constraints such as, table names, fields, data types, and the relationships between these entities

// A collection of tables is called a schema. A schema defines how the data in a relational database should be organized. In relational databases, you typically have to define your schema up front and the database server will make sure that any data being written to the database conforms to that schema.

30
Q

What is a table?

A

A table is a collection of related data held in a table format within a database. It consists of columns and rows. In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect.

31
Q

What is a row?

A

In relational databases, a row is a data record within a table. Each row, which represents a complete record of specific item data, holds different data within the same structure. A row is occasionally referred to as a tuple.

32
Q

What is SQL and how is it different from languages like JavaScript?

A

SQL is a declarative programming language. Declarative programming is when you write your code in such a way that it describes what you want to do, and not how you want to do it. You “declare” your intent and the Web browser does its best to output the desired results according to a predefined set of rules.

33
Q

How do you retrieve specific columns from a database table?

A

With select statement (specify atribute name).
// Querying a database typically takes the form of a select statement.

34
Q

How do you filter rows based on some specific criteria?

A

Filtering Results
Sometimes we want to get a subset of all rows in a table. Considering our original table of data, it is possible to specify which rows should be returned. This is done using a where clause.

35
Q

What are four comparison operators that can be used in a where clause?

A

=, <, >, and !=

36
Q

How do you limit the number of rows returned in a result set?

A

SQL provides us with the ability to limit the number of rows included in a result set.

37
Q

How do you retrieve all columns from a database table?

A

To select all of the columns in a table by replacing the list of column names with an * asterisk

38
Q

How do you control the sort order of a result set?

A

With “order by” clause in the select statement to control the order of the result set. The default sort order of the results is ascending order.

39
Q

What is a tuple?

A

In a relational database, a tuple is one record (one row). See record and relational database. A set of values passed from one programming language to another application program or to a system program such as the operating system.

40
Q

How do you add a row to a SQL table?

A

To add a row to this table, we would likely execute the following command:

insert into “table name” (“attribute name”, “attribute name”)
values (‘values, ‘values).

41
Q

How do you add multiple rows to a SQL table at once?

A

insert into “table name” (“attribute name”, “attribute name”), (“attribute name”, “attribute name”)
values (‘values, ‘values), (‘values, ‘values);

42
Q

How do you get back the row being inserted into a table without a separate select statement?

A

With returning * statement

43
Q

How do you update rows in a database table?

A

With SQL update statement.
update “table name”
set “attribute” = ‘value’
where “attribute” = ‘value’; (include a where clause in your update statements to only target specific rows)

44
Q

Why is it important to include a where clause in your update statements?

A

If “where” is not included, it would update every row in the table

45
Q

How do you delete rows from a database table?

A

With SQL delete statement.
delete from “table name”
where “attribute” = ‘value’
and “attribute” < value
returning *;

46
Q

How do you accidentally delete all rows from a table?

A

delete from “table name”;
If you don’t target specific rows to delete by including a where clause.

47
Q

What is a foreign key?

A

A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key links these two tables.

48
Q

How do you join two SQL tables?

A

select ..
from “table name1”
join “table name2” using (“column1//key”);

from “table name1”
join “table name2” on “table name1”.”column1” = “table name2”.”column1”

49
Q

How do you temporarily rename columns or tables in a SQL statement?

A

renaming them just for the result set using the as keyword

select “p”.”name” as “product”,
“p”.”category”,
“s”.”name” as “supplier”,
“s”.”state”
from “products” as “p”
join “suppliers” as “s” using (“supplierId”);

50
Q

How do you temporarily rename columns or tables in a SQL statement?

A

renaming them just for the result set using the as keyword

select “p”.”name” as “product”,
“p”.”category”,
“s”.”name” as “supplier”,
“s”.”state”
from “products” as “p”
join “suppliers” as “s” using (“supplierId”);

51
Q

What are some examples of aggregate functions?

A

max(), count() - to count rows, min(), sum(), and every()

52
Q

What is the purpose of a group by clause?

A

To separate rows into groups and perform aggregate functions on those groups of rows. This is done with a group by clause.

53
Q

What are the three states a Promise can be in?

A

A Promise is in one of these states:

pending: initial state, neither fulfilled nor rejected.
fulfilled: meaning that the operation was completed successfully.
rejected: meaning that the operation failed.

54
Q

How do you handle the fulfillment of a Promise?

A

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

// Fulfilled is a state of a Promise. It means that the promise has been resolved and now has its resolved value (using the internal resolve function). The operation represented by the promise has been completed successfully.

55
Q

How do you handle the rejection of a Promise?

A

We must handle the promise rejection. We do so using the catch() method of the promise:
thePromise
.catch(error => {
console.error(error)
})
We must always add a catch(), otherwise promises will silently fail.

56
Q

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

A

The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.

57
Q

What does express.static() return?

A

Parameters: The root parameter describes the root directory from which to serve static assets. Return Value: It returns an Object. OR returns an HTTP 404 if it can’t find a file

58
Q

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

A

The directory name of the current module
__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.