Introduction Flashcards

1
Q

What is NodeJS?

A

Node is a JS run time that allows you to run code on the server.

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

What engine does Node use?

A

Node uses the V8 engine and adds JS features.

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

In Node how would you work the file system?

A

In Node.js, you work with the file system by importing the fs (file system) module. This module provides an object with methods that allow you to interact with the file system.

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

How do you import modules in Node?

A

You import modules by using the require keyword.

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

What is the purpose of JavaScript in the backend, and why do we use it in the backend?

A

We use JavaScript for databases, authentication, input validation, business logic, and more. This is done to ensure that clients cannot view or manipulate the server-side code.

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

How do clients access our code since it’s in the backend?

A

Although they can not directly access it, they can communicate with it using the request and response pattern.

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

What are other purposes you can use Node for?

A

You can use it for more than just server-side code. You can use it for utility scripts, build tools, etc.

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

What is Node’s role in web development?

A

We use it to create a server and listen to incoming requests. Additionally, we use it to handle business logic, such as handling requests, validating input, connecting to databases, etc. Finally, we handle the responses, such as returning JSON or HTML.

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

What are the core modules in Node

A

HTTP, HTTPS, FS, PATH, AND OS

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

Using the HTTP module, how would you create a server, listen for requests, and send responses? Try to create a server yourself.

A

Create a server you call the createServer method. This method takes in a function. This function takes in two arguments, a response and a request. this function will run every time a request reaches our server.
This createServer method returns a server. We store this server into a variable in then we can call methods on this server. One of these methods is the listen. This method will keep our script running to listen for any incoming requests. This method takes in two arguments, the port and the host name.

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

Why is the event loop important in Node?

A

Node.js depends on the event loop. This is because the event loop will keep running as long as there are event listeners triggered. One of those event listeners is the callback function in the createServer method. This function is an ongoing event listener.

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

When using the request object, what are the important methods and properties?

A

The most important properties and methods are URL, METHOD, and HEADERS. The URL property indicates the requested URL. The method property specifies the HTTP method used (e.g., GET, POST). The headers property provides details about the request headers, such as the host, connection, and accepted content types.

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

How can we send data back in Node?

A

In Node.js, we can send data back by using the response object. First, we call the setHeader method on the response object to specify the content type we want to send back. For example, we might send HTML. After setting the content type, we can use the write method on the response object to send the HTML content. Finally, we call the end method to complete the response.

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

How do we change the status code and what is the status code for redirecting?

A

The code for redirecting: res.statusCode = 302;

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

How can we redirect someone in Node?

A

res.setHeader(“Location”, “/”);

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

What method is used to listen for events on a request object?

A

In Node.js, req.on is used to listen for events on a request object (req). When you use req.on(‘data’, callback), you’re saying, “Hey, whenever there’s incoming data on this request, run this callback function.” It’s commonly used for handling data streams, like when uploading files or processing large amounts of data from a client.

17
Q

When we have received all the data in the “data” callback function, what can we do?

A

You use req.on(‘end’, callback), you’re saying, “Hey, when the incoming data is all received, run this callback function.” It signals that the entire request body has been received, and you can now process or handle that data in the callback function. It’s commonly used to finalize processing after receiving all the chunks of data.

18
Q

What is a buffer?

A

In Node.js, a Buffer is like a container for raw binary data.

19
Q

When processing our data in the “data” callback function, what form does the data arrive in?

A

The data arrives as a buffer.

20
Q

How do we convert our buffers into real data?

A

Take an array of Buffer objects (body) and join them into a single Buffer. Then, .toString() converts that combined Buffer into a string.

21
Q

What is event-driven code?

A

In event-driven programming, actions are triggered by events.

22
Q

Why do we use the return statements on certain request methods?

A

We use return here to ensure that after handling the request, we exit the function and don’t continue executing code further down.

23
Q

In Node, what execution style does it revolve around itself?

A

The concept of event emitters and listeners.

24
Q

What are the drawbacks of using writeFileSync?

A

writeFileSync is synchronous, meaning if the operation takes a long time, it will block other code execution. Instead, we should use writeFile. This method accepts three arguments: the file name, the data, and a callback function. The callback function handles errors in case the task fails. Inside this callback function, we specify the code we want to execute once the file is written.

25
Q

What type of events does the event loop handle and what does it not handle?

A

The event loop handles fast-loading events. It does not handle larger events.

26
Q

What is responsible for large events?

A

hey are sent to the worker pool. The worker pool is also managed by Node. This worker pool is responsible for all the heavy lifting.

27
Q

Where is the worker pool?

A

This worker pool is sort of detached from your code because it runs on different threads.

28
Q

What happens once the worker is done loading the bigger content?

A

Once this worker is done, it will then trigger the callback. So, since the event loop is responsible for the callback functions, it will end up in the event loop.

29
Q

Tell me about the process of the event loop.

A
  1. The event loop starts by checking for any timer callbacks.
  2. it checks for any other pending callbacks, which may include blocking and long-blocking operations.
  3. After completing these callbacks, it enters the poll phase. In this phase, it retrieves new I/O events and executes their callbacks. Additionally, it may check for any deferred callbacks or jump to the timers phase if necessary.
  4. Following the poll phase, the check phase occurs, during which setImmediate callbacks are executed.
  5. Finally, the event loop enters the close callbacks phase, where it executes all close event callbacks.
30
Q

How do we create a package.json file in our project?

A

You can create a package file by typing npm init into the terminal. This will create a package.json file for your project. In this file, you can add scripts, such as a start script. This will allow you to start your applications faster.

31
Q

What are dependencies?

A

These are third-party packages that are not included with Node.js. These packages enable you to perform various functions, such as parsing data or validating input. Some of these dependencies are meant to be used for development, while others are for production. For example, nodemon is meant to be used in development. The –save flag will install a package as a production dependency, and the –save-dev flag will install a package only for development. There is also -g this will install everywhere in your machine.

32
Q

What is Nodemon?

A

Nodemon is a dependency that automatically watches for file changes and restarts the application if any are detected. This is convenient because we don’t have to spend time exiting our server whenever there is a change.

33
Q

What are the three types of errors?

A

Syntax, Runtime, and logical.