NodeJS Flashcards
What is Node JS?
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.
How does Node work?
Node is a single threaded which in the background uses multiple threads to execute asynchronous code.
When a request arrives, that single thread is used to handle that request. If that request takes time, the single thread moves to serve another request.
When the 1st request is finished it puts a message in an Event Queue (which node is continuosly monitoring in the background
Which are the apps to go with node?
- Because of being able to execute asynchronous code node is ideal for apps that include a lot of disc or network access (I/O-intensive apps).
- In the contrast, node should not be used in CPU-intensive apps (video-encoding, etc.)
What are the basic modules that are built in the core of node?
os - operating system fs - file system events http path process query strings stream
What is the scope of variables in nodeJS app?
Contrary to the browser environment, where variables are declared in the global (window) object, these variables are not declared in the global. scope in node.
They are scoped only to the file, they are in.
How to use variables outside of a module?
In order to use a function or variable outside of the node module, we need to explicitly export it (make it public).
How do you export and import functions outside of modules?
You can export functions and variables, by attaching them to a modules.exports object. By exporting functions, we make them public, however, they still maintain access to the variables that were defined in the module they are in
modules.exports.log = log;
To load a module, we use require() function.
const {log} = require(‘./logger’);
What is a wrapper function when it comes to working with node modules.
Node modules wraps its modules inside of an IIFE function.
function(exports, require, module, __filename, __dirname){};
What does path module do?
The path module provides utilities for working with file and directory paths. It can be accessed using: const path = require('path')
1) The path.parse() method returns an object whose properties represent significant elements of the path. Trailing directory separators are ignored, see path.sep.
The returned object will have the following properties:
dir root base name ext
2)
What are environmental variables?
Environmental variables, are generally where we store sensitive information (passwords, logins, usernames, apis). That should never go to your github.
They are located in variables.env
Node package require(‘dotenv’).config({path: ‘variables.env’}) allows us to access whatever is in variables.env and access it by process.env.nameOfVariable.
Environment variables do not need to be imported into files across the app -> They are located on the node process and are accessible from anywhere.
What is routing?
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
What are the differences between asynchronous and synchronous methods for file system module?
const fs = require(‘fs’);
All of the methods in the filesystem module come in synchronous and asynchronous version.
We should always use asynchronous version, because we do not want to block the single thread that node uses to requests from the clients.
What are middleware functions?
Middleware functions are functions that have access to the req, res objects and the next middleware function in the applications cycle.
Middleware functions are functions that are inbetween req and res and can perform some actions (like executing any code, make changes to the res, req objects). If the middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function.
We pass the middlewares functions to the router.METHOD, those middlewares are specific for an endpoint.
However, we may also define global middlewares. In order to do that we use const express = require('expess') const app = express();
app.use keyword, in which we pass a callback function.
How do we read and write to files using node?
const fs = require(‘fs’);
fs. readFileSync(‘./path’, {encoding: ‘utf-8’});
fs. writeFileSync(‘./path’, textOut);
With node, all asynchronous versions of the methods are much more popular.
When it comes to fs.readFile() and fs.writeFile(), we need to provide a callback function as the last argument, which in case of readFile accepts two arguments (err, data1) =>
if (err) return console.log(err);
In case of write, we need to only specify the error.
How do you start a webserver in node?
In plain node, we need to declare a server variable, by using const server = http.createServer((req,res) => res.end('')) from http module. Then we need to server.listen(port,'127.0.0.1', callback);