Node.js Flashcards
What is Node.js?
A program that allows JavaScript to be ran outside of the web browser.
What can Node.js be used for?
It is used to build back ends for web apps, command-line programs, or any kind of automation.
What is REPL?
Read-Evaluate-Print Loop
- Known as interactive toplevel or language shell
- Interactive computer programming environment that takes single user inputs, executes it, and returns the result to user
When was Node.js created?
Node.js was created in May 27, 2009 by Ryan Dahl
What back end languages have you heard of?
Node.js, React.js, MongoDB, Angular, Spring Boot
What is a computer process?
When a computer program gets executed by one or many computer threads.
Roughly how many computer processes are running on your host operating system (Task Manager or Activity Monitor)?
555 Processes
Why should a full stack Web developer know that computer processes exist?
Full stack needs all three processes (front-end, back-end, and data storage) need to be working in order for web apps to work
What is the process object in a Node.js program?
A global that provides info and control over the current Node.js process.
How do you access the process object in a Node.js program?
Can call process whenever since it’s global
What is the data type of process.argv in Node.js?
An array
What is a JavaScript module?
A single .js file.
What values are passed into a Node.js module’s local scope?
An object
Give two examples of truly global variables in a Node.js program.
process
console
What is the typeof and value of exports?
object
{ }
What is the typeof and value of require?
function Module { id: '.', path: '/absolute/path/to', exports: {}, parent: null, filename: '/absolute/path/to/entry.js', loaded: false, children: [], paths: [ '/absolute/path/to/node_modules', '/absolute/path/node_modules', '/absolute/node_modules', '/node_modules' ] }
What is the typeof and value of module?
object
Module { id: , path: , exports: { }, filename: , loaded: , children: [ ], paths: [ ], }
What is the typeof and value of __filename?
string
directory/directory/fileName
What is the typeof and value of __dirname?
string
directory/directory/directoryName
What is the purpose of module.exports in a Node.js module?
To import a specific function from a different .js file into the main .js file.
How do you import functionality into a Node.js module from another Node.js module?
const functionName = require(./fileName);
What is a directory?
A folder that holds files denoted by a /
What is a relative file path?
A relative file path is a portion of the file path, typically when file paths are in the same root directory
What is an absolute file path?
The full directory with all of its children/grandchildren/etc
Starts with a /
What module does Node.js include for manipulating the file system?
The fs module
How to access fs?
const fs = require(‘fs’);
What method is available in the Node.js fs module for writing data to a file?
fs.writeFile
Are file operations using the fs module synchronous or asynchronous?
There are both.
Synchronous does not take a callback
(should never be used on servers)
Asynchronous takes a callback
What is a client?
Software that requests content/resources from a server.
The ones who initiate the communication.
Example:
You go on a webpage and wait for the server to show the webpage
What is a server?
Software that shares content/resources to the client.
Waits for the client to send in requests.
Example:
Loads up a webpage when client is searching a webpage.
Which HTTP method does a browser issue a web server when you visit a URL?
GET
What is on the first line of an HTTP request message?
HTTP Method, target, and protocol version
What is on the first line of an HTTP response message?
Protocol version, status code, and string translation of status code
What are HTTP headers?
Meta data that comes with a request or response
Is a body required for a valid HTTP message?
Not all requests/responses need a body because there are some methods that just fetch resources and thats it.
For responses its because there are status codes that answer the requests without needing a body (201/404)
What is a host?
The hardware that can hold server/client software
What is NPM?
Node Package Manager
World’s largest software registry. Used to share and borrow packages.
It is made up of:
- The website
- The CLI
- The registry
What is a package?
A directory with one or more files that contains reusable code and a package.json file
How can you create a package.json with npm?
Go to the directory you want the package in and type in:
npm init –yes
What is a dependency and how do you add one to a package?
A dependency refers to the package in the library that cannot work without that package.
To add one to a package be on the directory that is holding the npm package in the CLI and then type in:
npm install dependencyName
What happens when you add a dependency to a package with npm?
Loaded into the node modules directory
List of dependencies are updated as well
What is a registry?
A large public database of JavaScript software and meta-information surrounding it
How do you add express to your package dependencies?
npm install express
What express application method starts the server and binds it to a network PORT?
The listen() method
What is express?
Express is a framework for Node.js.
It provides a set of features for web/mobile applications
How do you mount a middleware with an Express application?
use method of the app object
const express = require('express') const app = express();
app.use((req, res, next) => {
console.log(‘Time: ‘, Date.now());
next();
});
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
req and res parameters
What is middleware?
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.
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
app.JSON
What does the express.json() middleware do?
Parses incoming requests and returns JSON data into request.body
When would you need the express.json() middleware?
To get specific JSON data that matches the request where the content type header matches the type option
What is the significance of an HTTP request method?
The form of communication between client and server which decides what action is to be performed
What is a webpack?
A module bunder.
A tool that lets you bundle JavaScript applications/files
How do you add a devDependency to a package?
npm install package-name –save-dev
What is an NPM script?
A property in the package.json object that lets you run npm files.
How do you execute Webpack with npm run?
Use the property of the scripts object.
“scripts”: {
“build”: “webpack”
}
When on the CLI type in:
npm run build
What does express.static( ) return?
It returns a middleware function
An example of this would be: const middlewareFunction = (req, res, next) => { // code block };
What is the local __dirname variable in a Node.js module?
The directory name is the absolute path that the current module is in
What does the join( ) method of Node’s path module do?
Joins all given path segments together using a platform-specific separator as a delimiter.
What does fetch( ) return?
Returns a promise for a response
What is the default request method used by fetch( )?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
Specify in the 2nd part of the argument.
const fetchResponsePromise = fetch(resource [,init])
Example: fetch('https://jsonplaceholder.typicode.com/users', { method: 'GET', // or method: 'POST' })