NodeJS Flashcards
What is NodeJS?
NodeJS is a Javascript runtime that works for backend programming
NodeJS extends from an engine, what engine?
V8 Google engine, used to mainly in google chrome and NodeJs
Which language is V8 written in?
written in C++, to support Javascript.
How to execute Node from Cmd?
typing node
requiring a library that deals with file i/o
const fs = require(‘fs’)
creating a file synchronously with some content
fs.writeFileSync(‘filename.txt’, ‘filecontent’);
One example of development that is not associated with developing for server-side with NodeJS
Dealing with local files to process Building of production versions, ReactJs uses node indirectly for that, creating utility scripts in general.
What are the two ways of executing Node code?
by executing files (node (filename)) or REPL:
Read, Evaluate, Print, Loop, which is a way of executing code on the go in the command line, good playground.
Module to help with path concatenations
path, path.resolve(path1, path2)
The NodeJS event loop consists of 5 basic processes, the first step is
- Execution of timer functions (setTimer, setInterval, etc);
The NodeJS event loop consists of 5 basic processes, the second step is
- Execute pending (non-timer) callbacks from previous loops, such as I/O related callbacks and other events
The NodeJS event loop consists of 5 basic processes, the third step is
- Fetch new (non-timer) Event callbacks such as new connections and try to execute them right away, Or defer them to be executed in the next loop (2nd step)
The NodeJS event loop consists of 5 basic processes, the forth step is
- execute setImediate() callbacks, these are callbacks that execute always in the same loop, but always after all previous steps
The NodeJS event loop consists of 5 basic processes, the last step is
- Execute all ‘close’ event callbacks
What can happen when a long operation has to be executed by NodeJs?
It transfer the operation to the poll workers, which is carried with the work of deferring the process to another thread, the workers make sure this process happens automatically.
How to serve a file with the absolute path and the .html file to a user?
const path = require('path'); res.sendFile(path.join(\_\_dirname, '/view/page.html');
What does express.use(‘/admin’, adminRouter); does?
appends the path ‘/admin’ to all routes inside adminRouter.
Difference between path.resolve() vs path.join()
path. resolve is absolute, gets the root directory all the way from C:\
path. join starts with ‘./’, is relative, plus it converts the path in a way that works for both linux and windows.
consider: path.join(__dirname, ‘..’, ‘views’, ‘index.html’);
why is using ‘..’ instead of ‘../’ useful?
Because it goes up 1 directory level while preserving compatibility between different OS’s paths structure
what does path.dirname(string dir) returns?
returns the current already formated path, good for modularization and helper functions.
how to retrieve the application main entry point defined in package.json with the ‘process’ global var?
proccess.mainModule.filename
express().set() and express().get() does
sets a global variable that express may or may not understand, like: app.set(‘var_name’, ‘value’)
express.get(‘var_name’) will return it globally.
setting a templating engine with express
express().set(‘view engine’, (engine));
‘view engine’ is a reserved express global configuration option.
(engine) configures one of the templating engines that have built-in compatibility with express, such as pug, ejs.
(optional) telling express where is the root folder of the views:
express().set(‘views’, ‘(folder_path)’) // Default path location is ‘views’.
method to build on-the-fly and send to the user a dynamically outputted html page
res.render();