Master Flashcards
What is Node.js, and how is it different from other server side technologies?
Node.js is an open-source, cross-platform, server-side JavaScript runtime environment.
Node.js is different from other server-side technologies in a few key ways:
–> Language: It uses Javascript as its programming language. this means that developers can use the same language for both front-end and back-end development, which allows for a streamlined development process and reduced learning curve for juniors.
–> Non-blocking I/O model: allows a single process to serve multiple requests at the same time. Makes it great for real-time data processing like chat or streaming services.
–> Package Management: Node.js comes with a built-in package manager called npm (Node Package Manager), which allows developers to easily install, manage, and share reusable packages of code.
How do you handle asynchronous programming in Node.js?
Asynchronous programming is typically handled by using callbacks, promises and/or async/await.
–> Callbacks: A callback is a function that is passed as an argument to another function and is called when that function completes its operation. Can lead to nested callbacks (callback hell).
// example 1
fs.readFile(‘file.txt’, UTF-8, function(err, data) {
if (err) throw err;
console.log(data);
});
–> Promises: A Promise represents the eventual completion or failure of an asynchronous operation and can be in one of three states: pending, fulfilled, or rejected. Promises make it easier to write asynchronous code that is more readable and less error-prone.
// example 2
const fs = require(‘fs’).promises;
fs.readFile(‘file.txt’)
.then(data => console.log(data))
.catch(err => console.error(err));
–> Async/Await: The async keyword is used to define a function as asynchronous, and the await keyword is used to wait for a Promise to resolve before proceeding with the next instruction.
// Example 3
const fs = require(‘fs’).promises;
async function readData() {
try {
const data = await fs.readFile(‘file.txt’);
console.log(data);
} catch (err) {
console.error(err);
}
}
readData();
What is an event loop?
The event loop is responsible for managing all the asynchronous and non-blocking operations in Node.js.
The event loop works by continually monitoring the call stack - The call stack is where functions are executed in a first-in, first-out (FIFO) order. When a function is called, it is added to the call stack, and when it returns, it is removed from the stack.
Overall, the event loop is a crucial component of Node.js, as it allows Node.js to handle multiple requests and connections simultaneously without blocking the execution of the program.
In the context of OOP, what is a trait?
a trait is a grouping of methods and properties that can be reused in different classes. Traits are a way to implement code reuse in OOP without creating a new class hierarchy. ties into the idea of code “composability”.
// Example
–> Mammal class
–> Human Class
–> Humanoid Class
Since both the human class and humanoid class would share similar “traits”, but the humanoid class can’t inherit the mammal class, we may opt to abstract some of the methods/properties into a trait that both humans and humanoids can access.
How do you go about designing a database? What things do you consider?
1.) identify the requirements of the database - understanding the data that needs to be stored, talking to stakeholders about what they expect to be stored, analyze
2.) Entity-relationship diagram - This is a conceptual model of the database. Identify the “objects” that the database will be holding and the relationships between them –> eg. Users, Products, Orders, Shipments. A User has many orders, and order has many products.
3.) Define the schema: This involves defining the tables, columns, and relationships between the tables. Each table should have a primary key, which is a unique identifier for each row in the table, and other columns should use corresponding data types –> DATE, VAR_CHAR, TEXT etc…
4.) Normalize the data - the process of cleaning up the data to minimize redundancy and improve data integrity. Breaking down the data into smaller, more manageable tables. Eg. instead of having the products information stored in the orders table, create a products table and reference the product id in the order table with a foreign key.
What does your ideal local environment look like? How would you set it up?
My ideal local environment setup would consist of:
–> MacOS operating system
–> Visual Studio Code for my text editor
–> Using Docker in a team setting with a Docker image that had Node, Postgres, Express, React on it. In a personal setting I would probably just run these locally.
–> NPM for package mgmt.
–> React debugger on my browser console
–> Mocha/chai testing framework installed as well.
What resources do you use to stay up to date with new technologies?
–> Reddit (r/webdev, laravel, learningprogramming, React, javascript)
–> medium
–> Dev.to
–> Twitter (sometimes)
How do you organize your coding projects?
1.) Create a root directory
2.) Use subdirectories to organize related files:
–> Client folder that holds views/templates, components, hooks, public assets like css or images.
–> Server folder that holds routes, .env, database scheme, seed file, queries.
2.) Use a consistent naming convention between files or follow framework best practices.
3.) Make sure to initialize git in the root directory.
4.) initialize npm and your package.json in both your client and server directories.
It also really depends on the framework you are working with - Something like Ruby on rails or Laravel would have you organize your code into Models, View, Controllers, Factories, Routes which would be a little different then how you would setup a Express project.
Can you explain the difference between callbacks, promises, and async/await?
Callbacks, Promises, and Async/Await are three different ways to handle asynchronous operations in JavaScript.
–> Callbacks: are functions passed to other functions, and are executed once an asynchronous operation is complete.
–> Promises: represent a value that is is not available yet, but may be in the future. A promise has three states: pending, fulfilled or rejected. Promises can be chained using the .then() and .catch() methods to handle success and failure cases.
–> Async/Await: Async functions return a Promise, and the await keyword can be used to pause the execution of the function until a Promise is resolved. Async/Await is typically easier to read as its written similarly to synchronous code.
What is the purpose of the package.json file?
A package.json file is very important to a node.js project because it handles:
1.) Dependancy Management - all the 3rd party libraries that your project uses.
2.) Project Configuration - version, description, license, development mode scripts.
3.) version control
4.) Publishing to npm registry.
How do you manage dependencies in a Node.js project?
The most popular way to manage dependencies in a Node.js project is by initializing the project with NPM (node package manager) which will create a package.json file in the root of your project.
Another option would be to use a similar dependancy manager like yarn, which has gained popularity over the years.
How do you handle errors in a Node.js application?
1.) using try/catch blocks in your code.
2.) Error-first callbacks - where the first argument of the callback function is an error object.
2.) Throw new Error - allows for a user defined exception that you can add to your functions to handle error states.
Can you explain the difference between the “require” and “import” statements in Node.js?
How do you use streams in Node.js, and what are some use cases for them?
How do you deploy a Node.js application to a production server?