Node.js + NPM + Express Flashcards
What is Node.js?
a program that allows JavaScript to be run outside of a web browser
commonly used to build back ends for Web applications, command-line programs, or any kind of automation that developers wish to perform
powered by V8– the same JavaScript engine in the Google Chrome browser
its primary focused on creating simple, easy-to-build network clients and servers
What can Node.js be used for?
primarily used for non-blocking, event-driven servers, due to its single-threaded nature
It’s used for web sites and back-end API services
used for outside of the browser
What is a REPL?
read–eval–print loop (REPL) is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user
simply type the node command to initiate the REPL session
When was Node.js created?
2009
What is a computer process?
the instance of a computer program that’s being executed by one or many threads (running tasks)
instance of a running program
threading- how the cpu remembers what it was doing
Why should a full stack Web developer know that computer processes exist?
Full stack Web development is based on making multiple processes work together to form one application, so having knowledge of computer processes is important
if were writing front end code and the site is connecting to backend, we have to know how to work with backend (node.js, postgress) as well, thats multiple process
What is the process object in a Node.js program?
The PROCESS object is a GLOBAL OBJECT that provides information about, and control over, the current Node.js process
As a global, it is always available to Node.js applications without using require()
you can handle command line arguments with the process object using process.argv
How do you access the process object in a Node.js program?
just typing process in the terminal/console since process is a global object
What is process.argv in Node.js? And what does it return?
process.argv returns an array containing the command line arguments passed when the Node.js process was launched
returns an array of strings
What is a JavaScript module?
in JS, a module is a single .js file
Node.js supports modules using a system heavily influenced by CommonJS
Authors of Node.js programs strive to separate their code into modules that each provide a small chunk of functionality
The program as a whole is the result of all of these modules working together in concert.
What values are passed into a Node.js module’s local scope?
exports, require, module, __filename, __dirname
directory and file name of the current module
Give two examples of truly global variables in a Node.js program.
processed, console
What is the purpose of module.exports in a Node.js module?
to export the current file its in
allows variables from one file to be used in another file
How do you import functionality into a Node.js module from another Node.js module?
with required() and passing in a relative path to the file
What is the JavaScript Event Loop?
event loop’s job is to look at the stack and the task queue, if the stack is empty, it takes the first thing on the queue and pushes onto the stack then runs the code in the stack
What is different between “blocking” and “non-blocking” with respect to how code is executed?
blocking executes the code in series. You cant continue until an operation (http request for ex) is complete
In Node.js doc: blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes.
non-blocking refers to code that doesn’t block execution
blocking methods execute synchronously while non-blocking methods execute asynchronously
What is a directory?
is known as a folder or a collection or files
What is a relative file path?
a location in the current working directory (or the present working directory/ pwd)
relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory
What is an absolute file path?
location from the root directory (starts w a /)
all existing directories is in the root directory
in other words, you can say that an absolute path is a complete path from start of actual file system from / directory
What module does Node.js include for manipulating the file system?
fs module
fs module provides an API for interacting with the file system
What method is available in the Node.js fs module for writing data to a file?
writeFile method
Are file operations using the fs module synchronous or asynchronous?
async by default but sync
but it says that fs.writeFileSync returns undefined?
What is NPM?
its a software registry
developers use NPM to share and borrow packages to solve a particular problem
npm consists of three distinct components (when people talk about NPM, they could be talking about one of 3 things):
1) the website - to search packages
2) the Command Line Interface (CLI) - command line client / NPM client, to run/interact with npm from the registry
3) the registry - public database of information about packages people are sharing
What is a package?
reusable code
its a directory with one or more files in it that also has a file called file.json (with some meta data about this package)
How can you create a package.json with npm?
navigate to the root directory of your package and run the command “npm init” with the –yes or -y flag
-y flag when passed to NPM commands tells the generator to use the defaults instead of asking questions
npm init -y will simply generate an empty npm project without going through an interactive process
What is a dependency and how to you add one to a package?
a dependency is a specification of the packages your project depends on
by running npm install
What happens when you add a dependency to a package with npm?
it adds it to the package.json dependencies
How do you add express to your package dependencies?
by running command npm install express
What Express application method starts the server and binds it to a network PORT?
listen method
var express = require('express') var app = express() app.listen(3000)
How do you mount a middleware with an Express application?
using the use method of the app object
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
request and response objects
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
✧ what is Express?
npm package
a minimal and flexible Node.js web app framework
it is a routing and middleware web framework
an Express application is essentially a series of middleware function calls
✧ what is a middleware?
middleware is a function or program that is going to run between the time that the server gets the request and the time the server sends the request out to the client – middleware is anything that happens between the time the server gets the request and the time the server sends out a response
middleware functions have access to the request object (req), the response object (res), and the next middleware function in the app’s request-response cycle
middleware functions can perform the following tasks:
- execute any code
- make changes to the request and the response object
- end the request-response cycle
- call the next middleware function in the stack
✧ What is routing?
determines how an app responds to a client request to a particular endpoint, which is a URL (or path) and a specific HTTP request method (GET, POST, DELETE, PUT, etc)
syntax: app.METHOD(PATH, HANDLER)
✧ What is the purpose of res.json()? What arguments does it take?
sends a JSON response
this method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify()
the parameter can be any JSON type— object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON
✧ What is the use() method of Express application objects?
each app.use(middleware) is called every time a request is sent to the server.
app.use() acts as a middleware in express apps. Unlike app.get() and app.post() (& other app.METHOD), you actually can use app.use() without specifying the request URL. In such a case what it does is, it gets executed every time no matter what URL’s been hit
✧ What is the method that sends the HTTP response?
res.send()
✧ What is Route Parameters?
Route parameters are named URL segments that are used to capture the values specified at their position in the URL
the captured values are populated in the req.params object
What does the express.json() middleware do and when would you need it?
returns a middleware
it gives your app the ability parse json
we need it if we want our program to able to handle client request bodies that require json
What does the express.json() middleware do and when would you need it?
returns a middleware
it gives your app the ability parse json
we need it if we want our program to able to handle client requests that have json bodies (when the content type is application/json??)
What is Webpack?
runs on node.js
a tool that lets you bundle your js applications
called a module bundler
How do you add a devDependency to a package?
npm install webpack –save-dev
your webpack gets used when its being prepare/ build, not when its running, that why we only need –save-dev
run time vs build time
ex: we run express directly at run time
What is an NPM script?
key value pair in your package.json, they key is a short name for the value
a shortcut for a command line
How do you execute Webpack with npm run?
npm run scriptName
ex: npm run build
How are ES Modules different from CommonJS modules?
- ES Modules syntax is more compact
- their structure is flat rather than nested
- their module bundling is better than CommonJS modules
es module is part of the language now whereas commonjs is a node language
What kind of modules can Webpack support?
ES Modules and CommonJS modules