SERVER SIDE BACKEND Flashcards
What is the purpose of module.exports in a Node.js module?
It creates a module that can be imported into another file
How do you import functionality into a Node.js module from another Node.js module?
const, var , or let followed by a variable name then assign it a require(directory/filename)
SYNTAX: const add = require(./add) THE .js can be ommitted as it is implied.
What is the JavaScript Event Loop?
It pushes code that is in the callback Queue onto the call stack
What is different between “blocking” and “non-blocking” with respect to how code is executed?
Blocking is code that must finish before anything else can be done and non-blocking are code that is run immediately onto the stack; Javascript can make blocking into non-blocking by putting it to the side
Directory
a folder that holds file
relative file path
it is within the computer starting from current directory, relative to where you are from
absolute file path
it states the whole path from the root to where it is in the folder
What module does Node.js include for manipulating the file system?
it is fs module
can use require(‘fs);
What method is available in the Node.js fs module for writing data to a file?
fs.writefile
Are file operations using the fs module synchronous or asynchronous?
asynchronous unless stated as synch in the method
What is a client? What is a server?
The client is the one that requests information while the server hosts and wait for request to give the information
Which HTTP method does a browser issue to a web server when you visit a URL?
It uses the GET method
What is on the first line of an HTTP request message?
What is on the first line of an HTTP response message?
A start-line describing if request/response is successful or failed
FOR REquESt: a http method (get, put , post) and the request target usually an absolute path
for Responses: Status Line (protocol version, status code, status text_
What are HTTP headers? Is a body required for a valid HTTP message?
Headers: case insentiive string followed by a colon and a value depending on the type of header, General (via), response(vary and accept-ranges), and representation headers(content-type)
Bodies are not neccessary
What is NPM? What is a package?
It is a package manager, that is open sourced where people can send code or technologies and modify or use that code
A package is a building block that solves one problem and solves it well, a reusable code that can be implemented in custom projects
a Package is:
a) a folder containing a program described by a package.json file
b) a gzipped tarball containing (a)
c) a url that resolves to (b)
d) a @ that is published on the registry (see registry) with (c)
e) a @ (see npm dist-tag) that points to (d)
f) a that has a “latest” tag satisfying (e)
g) a that resolves to (a)
How can you create a package.json with npm?
navigate to the root path and then run : npm init
What is a dependency and how to you add one to a package?
Packages that it depends on whether it is a package lock, shirnkwrap file or yarn lock, the installation of dependencies will be driven by that npm install
What happens when you add a dependency to a package with npm?
It creates a folder in the node modules directory and add to the dependencies
How do you add express to your package dependencies?
npm install express
require(‘express’)
what Express application method starts the server and binds it to a network PORT?
listen method const app =express(); app.listen(3000, () =. { (code here) });
How do you mount a middleware with an Express application?
app.use() the first parameter is the path where the middleware function executes when the base of the req path matches path
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
(req, res) request and response; (request object is passed in)
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json which runs UTF encoding
What is the significance of an HTTP request’s method?
The significance is that it will determine what the response will be, it by letting the server know what to do do
What does the express.json() middleware do and when would you need it?
It is so that the app will be able to read and parse incoming requests with JSON loads Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option
What does express.static() return?
(it returns a function that
does HTTP 404 Not Found if the file is not there otherwise it returns your file.
What is the local __dirname variable in a Node.js module?
The directory name of the current module, it is the same path as (path.dirname(__filename));
What does the join() method of Node’s path module do?
Joins all given path segments together
EXample SetUP: const joinedPath = path.join(\_\_dirname, 'public'); app.use(express.static(joinedPath));