Nightly Review Flashcards
What is Array.prototype.filter for?
-creates a new array with all elements that pass the condition or test you specify
What is Array.prototype.map useful for?
- Allows you to quickly iterate and manipulate values of an array
- for example, double every element in the array
- creates a new array as well
What is the process object in a Node.js program?
- it’s a global that provides information about, and control over the current node.js processes
- it is always available to node.js applications without using require
- global means it is available in any files, without having to declare it
How do you access the process object in a Node.js program?
- you can explicitly access it using require(‘process);
- b/c it is a global, it is always available w/out require
What is the datatype of process.argv in Node.js?
- returns an array of strings
- it contains the command line arguments passed when the node.js process was launched
What is the JavaScript Event Loop?
- it is a “handler”
- it looks at the stack and the task queue, and if the stack is empty it takes the first thing on the queue and pushes it onto the stack
- it allows JavaScript to run tasks concurrently
What is the difference between “blocking” and “non-blocking” with respect to how code is executed?
- blocking code is code that runs within the stack
- non-blocking code is code that is put into the callback queue
What is a JavaScript module?
- a module is a single JS file
- each file in node.js is treated as a separate module
What values are passed into a Node.js module’s local scope?
-exports, require, module, __filename, and __dirname
Give two examples of truly global variables in a Node.js program
-global, process, setTimeOut, setInterval, Buffer
What is the purpose of module.exports in a Node.js module?
-to use functions, objects, or arrays in other modules
How do you import functionality into a Node.js module from another Node.js module?
-require function, and then the path of the file
require(‘./filename’);
What is a directory?
-a file that lists other files
What is a relative file path?
- it’s the path towards your file from wherever you are
- starts with ./ (or can leave it off, the ./ is implied)
What is an absolute file path?
The full path, starting at the root of your system
What module does Node.js include for manipulating the file system?
the file system module
What method is available in the Node.js fs module for writing data to a file?
writeFile method
Are file operations using the fs module synchronous or asynchronous?
both!
writeFile is async,
writeFileSync is sync
What is JSON?
- it is a string
- text based data following javascript object syntax
What is serialization and why is it useful?
- serialization: converting a native object to a string so it can be transmitted across the network
- the process of turning an object in memory into a stream of bytes so you can store it on a disk or send it over a network
- allows you to convert your data into JSON and communicate with servers
What is deserialization and why is it useful?
- deserialization: converting a string to a native object
- the reverse: turning a stream of bytes into an object in memory
How do you serialize data into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string using JavaScript?
JSON.parse()
What is a client?
- a computer or software that accesses a service made available by a server
- something that sends a request
What is a server?
-a computer program or device that provides a resource or service to a client
Which HTTP method does a browser issue to a web server when you visit a URL?
GET method
What is the format of an HTTP request message? (Review contents of flash card- July 7)
- Start-line (protocol version, status, status text)
- headers
- body (optional)
What is the format of an HTTP response message?
- status line(protocol version, status, status text)
- headers
- body (optional)
What is a computer process?
-an instance of a computer program that is being executed by one or many threads
How do you add express to your package dependencies?
npm init -y, then npm install express.
once you create a new package.json, you can include it in the dependancy
What Express application method binds the server to a network PORT?
app. listen();
- use require to pull in whatever the express package exports
what is a middleware function?
-functions that have access to the request object(req), the response object(res), and the next (next parameter) middleware function in the application’s request response cycle
How do you register a middleware with an Express application?
-call the use method, and pass in a callback as an argument
Example: app.use(callback-method here)
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
request object, and the response object
what does root: __dirname do?
it’s part of the options argument, and you can use it to check to see the relative directory instead of from the absolute file path
What is the content-type header of HTTP requests and responses used for?
to indicate the media type of the resource
What does express.static() return?
It returns a new middleware function
What is the local__dirname variable in a Node.js module?
the directory the file itself is in
What does the join() method of Node’s path module do?
it combines all specified path segments together
What is the appropriate Content-Type header for requests and responses that contain JSON in their bodies?
-application/ json
What does the express.json() middleware do and when would you need it?
- it parses the body of your request, and recognizes it as a json object
- if your server needs to receive JSON from a client, then you need to express.json() to work
What is the significance of an HTTP request’s method?
the method tells the server what the client is trying to do
What is Node.js?
- It’s a program that lets you run javascript outside of a web browser
- it lets you build back ends for web applications, command-line programs, or any kind of automation
- it is powered by v8; the same JavaScript engine in the google chrome browser
What can Node.js be used for?
-it is used to build scalable network applications