Node Flashcards
What is Node.JS?
A program that allows JavaScript to be run outside of a web browser
What can Node.JS be used for?
Commonly used to build back ends for Web Applications, command-line programs, or other automation software developers wish to perform
What is a REPL?
Read, Eval, Print loop —> also termed a ‘language shell’
When was Node.JS created?
May 27, 2009 —> created by Ryan Dahl
What backend languages have you heard of?
Python, Ruby, Java, PHP, C, C++, c# (.net), JavaScript (&Node)
What is the process object in a Node.JS program?
A global that provides information and control over the current node.JS process
What is a ‘process’?
Any one program that your computer is running at a given time
When you execute a file with node, you start a…
Process!
How do you access the process object in a Node.JS program?
It’s globally available, so you can just type ‘process’ (although it also can be required in)
What data type does process.argv return in node.JS?
It returns an array containing command line args passed in when the process was launched
What will process.argv[0] be?
The path to node’s executionable file
What will process.argv[1] be?
The path to the JavaScript file being executed
What will process.argv[2] onwards be?
Any command line arguments
Every index in the argv array always is… (data type)
A string
What is a JavaScript module?
A module is a single .JS file.
All modules are wrapped in a…
Function
What values are passed into a Node.JS module’s local scope via the wrapping function?
exports, require, module, __filename, __dirname
Give two example of truly global variables in a Node.JS program.
Process & console objects (global, setTimeout, setInterval)
What determines what is returned by .require(‘file-path’)?
Whatever is stored in the module.exports variable in the file specified.
Will variables between files collide?
No —> because each file is wrapped in a function, they have their own unique scope
What is the purpose of module.exports in a Node.JS module?
It allows you to export code out from one module
How do you import functionality into a Node.JS module from another Node.JS module?
- Set module.exports = ‘what-to-export’
- Use the require keyword with the file path passed in as the argument
What is the JavaScript Event Loop?
It allows JavaScript to run async events in a sequential order.
What is different between ‘blocking’ and ‘non-blocking’ with respect to how code is executed?
Blocking code ‘blocks’ the call stack —> while it’s running, nothing else can happen. Non-blocking code doesn’t stay on the call stack forever, allowing other things to run while it does work.
What does ‘single-threaded’ mean?
It can only run one thing at a time
What is a directory?
A unique type of file that contains travel information to access files or other directories
What is a relative file path?
The path to a file from where you are currently at
What is an absolute file path?
The path to a file from the root directory
What a module does Node.JS include for manipulating the file system?
The File system (fs) module
The require function always takes what data type?
String! Either a file path for a file OR a module name for a module
When using an absolute file path, instead of using a hard-coded path you can use…
The path module —> path.join(argument1, argument2)
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?
Depends on the operation! Typically asynchronous. Tbh ignore the synchronous ones.
What is npm?
The worlds largest software registry. Three components: website, CLI, and registry.
No longer called node package module —> used for more than just node.
What is a package in npm?
Reusable bits of code —> a directory with one or more files in it with a package.json file
How can you create a package.json with npm?
Run the command ‘npm init’ at the root directory of the package.
Adding —yes auto-fills the package.json file
What is a dependency and how do you add one to a package?
It’s a library (another package) your project (your package) requires to work properly. Using npm install ‘package’ will install the package as a dependency.
What happens when you add a dependency to a package with npm?
That dependency is added to the node_modules folder along with all files related to that package in addition to all of its dependencies. It also adds and it under dependencies in package.json