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”, “/”);