Node JS Flashcards
What is Node JS?
A cross-platform JavaScript runtime environment to build fast and scalable server-side applications. It embeds and adds additional functionality over Google Chrome’s V8 JavaScript engine. Node uses one main thread. It also uses events to sync states.
node.js cheetsheet
https://www.codecademy.com/learn/learn-node-js/modules/intro-to-node-js/cheatsheet
npm init
A command to create a Node.js project. This creates the package.json and gives you step-by-step prompts.
Use “npm init -y” to keep everything default.
npm
Allows you to download remote packages and manage them via terminal. It is a library and registry for JS software packages. It is the most popular package manager. It allows you to re-use your code in other projects, use other developer’s code, and share your solutions to other developers.
npm install
used to install dependencies from the package.json file.
-use “npm i PACKAGE_NAME” to install the dependency only on the current project.
-use “npm install -g PACKAGE_NAME” to install the dependency globally to be used in any project.
-use “npm i PACKAGE_NAME” with “-D” or “–save-dev” flag to install a package as a dev dependency.
npm start
starts the application.
How to quit the application?
On the active session, hold Ctrl+c.
setImmediate(functionToExecute)
A function used to execute a function right after the current event loop finishes. Essentially the functionToExecute is called after all the statements in the script are executed.
fs
A module responsible for all the async or sync file I/O operations.
fs.readFile()
A method used to read files on your computer.
require(“module_name”)
A function used to load and cache JavaScript modules. You can use destructuring if you only want to access specific objects or functions from an imported module.
The require function returns module.exports.
REPL
Read, Evaluate, Print, and Loop. To use REPL, type “node” and press [enter]. This is a quick way to test simple JavaScript code.
core modules
Core modules are built into Node.js environment. Some of the core modules are in the global scope so you do not need to require() them. Other core modules will need the require() function.
custom modules
custom modules are imported using require(“./xxxx”) and a specified file path.
process module
a global module that gives access to information about the Node.js runtime environment.
console module
exports a global console object allowing the terminal to act as a debugging console.
path module
a global module that provides utilities for working with file and directory paths.
util module
contains methods used to maintain and debug your code.
util.promisify() - takes an input for the function and returns them as a promise.
os module
provides methods to retrieve information about the computer, OS, and network interfaces.
fs module
a global module allowing you to work with your file system.
http module
a global module that creates an HTTP server that listens to server ports and gives a response back to the client.
package
a third-party module wrapped up with the list of that module’s own dependencies.
package.json
A manifest file containing metadata about a project and it’s dependencies.
- The “dependencies” list all the project’s dependencies alongside their version numbers. “devDependencies” indicates that packages are being used specifically for development.
- Anyone who wants to work with you on your project can download your package.json file and run “npm i”. This will install the listed dependencies they can use.
express js
a web application framework for Node.js that helps simplify the process of building web applications. It provides a set of tools and features for creating APIs, web servers, and web applications.
node modules with require, exports, and imports
https://adrianmejia.com/getting-started-with-node-js-modules-require-exports-imports-npm-and-beyond/
module.export starts off as an empty object.
revealing module pattern
A design pattern where we only want to expose the properties and methods needed via a returned object.
example:
var data = “123”
function showData() {
return data;
}
// here only the function is accessible.
module.exports = showData;
when code is ran through Node…
The code is wrapped in an IIFE, giving us access to the module object, require, etc. :
(function (exports, require, module, __filename, __dirname) {
// our code here
var greet = function() {
console.log(“Hello”);
}
module.exports = greet;
}
module.exports vs exports
https://stackoverflow.com/questions/16383795/difference-between-module-exports-and-exports-in-the-commonjs-module-system
event emitter
an object that can emit named events, which can be listened to by registered listeners. When an event is emitted, all listeners are invoked.
example:
const EventEmitter = require(“events”);
EventEmitter.on(“listen”, () => {
console.log(“hello”)
})
EventEmitter.emit(“listen”);
module, package, dependency
Pretty much interchangeable terms to describe re-usable code.
nodemon (3rd party module)
a tool that helps develop Node.js based applications by automatically restarting the node application when a file change in the directory is detected.
how to uninstall a node module
“npm uninstall PACKAGE_NAME”.
To uninstall all modules, delete the node_modules directory and delete the package-lock.json file.
streams
objects that let you read data from a source or write data to a destination in a continuous fashion.