Introduction Flashcards
What is NodeJS?
Node is a JS run time that allows you to run code on the server.
What engine does Node use?
Node uses the V8 engine and adds JS features.
In Node how would you work the file system?
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 do you import modules in Node?
You import modules by using the require keyword.
What is the purpose of JavaScript in the backend, and why do we use it in the backend?
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 do clients access our code since it’s in the backend?
Although they can not directly access it, they can communicate with it using the request and response pattern.
What are other purposes you can use Node for?
You can use it for more than just server-side code. You can use it for utility scripts, build tools, etc.
What is Node’s role in web development?
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.
What are the core modules in Node
HTTP, HTTPS, FS, PATH, AND OS
Using the HTTP module, how would you create a server, listen for requests, and send responses? Try to create a server yourself.
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.
Why is the event loop important in Node?
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.
When using the request object, what are the important methods and properties?
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 can we send data back in Node?
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 do we change the status code and what is the status code for redirecting?
The code for redirecting: res.statusCode = 302;
How can we redirect someone in Node?
res.setHeader(“Location”, “/”);
What method is used to listen for events on a request object?
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.
When we have received all the data in the “data” callback function, what can we do?
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.
What is a buffer?
In Node.js, a Buffer is like a container for raw binary data.
When processing our data in the “data” callback function, what form does the data arrive in?
The data arrives as a buffer.
How do we convert our buffers into real data?
Take an array of Buffer objects (body) and join them into a single Buffer. Then, .toString() converts that combined Buffer into a string.
What is event-driven code?
In event-driven programming, actions are triggered by events.
Why do we use the return statements on certain request methods?
We use return here to ensure that after handling the request, we exit the function and don’t continue executing code further down.
In Node, what execution style does it revolve around itself?
The concept of event emitters and listeners.
What are the drawbacks of using writeFileSync?
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.