Node.js Flashcards
What is Node.js?
Node.js is an open-source, cross-platform JS runtime environment
Running a javascript code without being in the web broswer
What can Node.js be used for?
Used for server-side programming
primarily deployed for non-blocking, event-driven servers like traditional websites and back-end API services
Build a back-end, make command-line app, automated scripts in JS
What is a REPL?
A read-eval-print loop:
- it reads, evaluates, prints the result and then loop it by writting again in the next line
examples: console.logs() & browser dev console
When was Node.js created?
2009 - 2011
What back end languages have you heard of?
ruby, PHP, python, C++, java, C, C#, JS, perl, kotlin
What is the process object in a Node.js program?
control over information that is executing the current node
How do you access the process object in a Node.js program?
You can just reference it because it is a global object
What is the data type of process.argv in Node.js?
it is an array of strings
What is a JavaScript module?
It is a specific individual file
What values are passed into a Node.js module’s local scope?
__dirname & __filename & exports & module & require()
Give two examples of truly global variables in a Node.js program.
global & process & console object
What is the purpose of module.exports in a Node.js module?
Module.exports are the instructions that tell Node.js which bits of code (functions, objects, strings, etc) to export from a given file so that other files are allowed to access the exported code
To expose values to grab other files
How do you import functionality into a Node.js module from another Node.js module?
const variableName = require(‘fileName’)
Pulling information from another module
What is the JavaScript Event Loop?
part of the JS runtime environment that pushes asynchronous callbacks onto the call stack when the call stack is clear.
example: ajax, delay time
What is the difference between “blocking” and “non-blocking” with respect to how code is executed?
blocking: operations that block further execution until that op finishes - need to wait for other things to finish - anything that is sitting on the call stack
- For example: for-loop
non-blocking: code that doesn’t block execution - other things will execute while waiting for this code to be executed when there’s like a delay
- HTTP request,
What is a directory?
a special file that lists files and directories
What is a relative file path?
the location that is relative to a current directory
What is an absolute file path?
specifying the location of a file or directory from the root directory - using a ‘/’
takes you to the file from anywhere
What module does Node.js include for manipulating the file system?
fs module
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?
both
What is NPM?
It’s a software registry. Open-source developers to share and borrow packages. It consists of these three things: website, cli, and registry.
What is a package?
directory with one or more files & package.json
How can you create a package.json with npm?
npm init –yes
What is a dependency and how do you add one to a package?
npm install ‘packageFile’
Dependency: a third-party code that your application depends on. piece of software that your software will need.
What happens when you add a dependency to a package with npm?
The package.json is updated with the new dependency and the package is downloaded from NPM registry to node_modules
How do you add express to your package dependencies?
npm install express
which Express application method starts the server and binds it to a network PORT?
app.listen()
listen - servers are listening to the incoming in that port
How do you mount a middleware with an Express application?
app.use((req, res))
call the use method
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
req & res
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
What does the express.json() middleware do and when would you need it?
it parses incoming requests with JSON payloads and is based on body-parser
We need this when we need to receive JSON from the API
What is the significance of an HTTP request’s method?
It gives specific action to be performed for its method request
What does express.static() return?
a path - returns a middleware
What is the local __dirname variable in a Node.js module?
name of the current module or the path to the current module
What does the join() method of Node’s path module do?
joins all given path segments together
What does fetch() return?
a Promise that resolves a Response object
What is the default request method used by fetch()?
GET
How do you specify the request method (GET, POST, etc.) when calling fetch?
fetch(‘ ‘, { method: ‘ ‘});
When does React call a component’s componentDidMount method?
immediately after a component is mounted (inserted into the tree).
Name three React.Component lifecycle methods.
componentDidMount()
componentDidUpdate()
componentWillUnmount()
How do you pass data to a child component?