Backend Flashcards
Web Framework
A software framework designed to support web application development
Express
A minimalist web framework for Node.js. Express abstracts away many difficulties working directly in Node.
Node.js
Node.js brings event-driven programming to web servers, enabling development of fast web servers in JavaScript. Developers can create scalable servers without using threading, by using a simplified model of event-driven programming that uses callbacks to signal the completion of a task.
Middleware
A function that an express server runs between receiving a request and responding to that request:
const middleware = (req, res, next) => { // Middleware function body };
(req, res, next)
The req parameter stands for request. Information and methods related to the incoming request will be stored in this object.
The res parameter stands for response. This object has information and methods related to sending back a response from the server.
The next parameter, when called, will tell Express that this middleware function is complete. It will then go on to the next piece of middleware.
Route
Also called a path, the part of the URL that comes after the domain name
Query String
Text that comes at the end of a URL and provides additional information to a given route
Query Parameter
A key-value pair in a query string, used to filter the resources requested from the server. The key and the value are strings separated by an equals sign =. In the below example, the query parameter key is q and the value is javascript.
https://www.google.com/search?q=javascript
Route Parameter
A part of the URL that changes depending on the data to be displayed on the website, used to access parts of a URL by storing the value in a variable.
RESTful APIs
A web API that adheres to the constraints of REST (Representational State Transfer - a set of constraints for building web APIs).
With REST, if you have a URL, then you have a resource. Resource refers to the data returned by a request; that data can be a text file, HTML page, image, video, JSON, or something else. Every URL provides access to a resource on your server.
A RESTful API server provides access to resources. A client, like the browser or another server, can then access and change the resources.
HTTP Request Method
Also called an HTTP verb, a method that indicates the desired action (such as deleting a resource) to be taken on a given resource such as GET, POST, PUT, PATCH, and DELETE.
API Endpoint
A location where a client can send a request to retrieve a resource that exists on the server
HTTP response status code
A code sent along with every HTTP response from the server, indicating the server’s response to the client
RESTful Design Principles
URLs should include nouns, not verbs.
Use plural nouns only for consistency (no singular nouns).
Use HTTP methods (HTTP/1.1) to operate on these resources:
Use HTTP response status codes to represent the outcome of operations on resources.
Should
CRUD
//(Route name, method, description)
(Create, POST, Create a new post, assign an id, and return at least the id)
(Read, GET, return post/profile with specified id, or return 404 if it doesn’t exist)
(Update, PUT, update an existing post with the data in the request body)
(Delete, DELETE, Delete post with specified id, or return 404 if it doesn’t exist)
(List, GET, Return a list of profiles/posts)
HTTP Status Codes
100 -199: Indicates an informational response; unlikely you will need to return a response in this range
200-299: Indicates success; request was received, understood, and processed.
300-399: Indicates redirection; a resource at a different URL has been subbed for the requested resource
400-499: Indicates a client error; there is a problem with the way the client submitted the request
500-599: Indicates a server error; the request was accepted, but an error on the server prevented request’s fulfillment
URL
Uniform Resource Locator
Controller File
A file that defines and exports the route handler functions and is responsible for managing the state of a single resource in an API
Express router
A modular middleware and routing system that can be attached to an Express app
Group-by-resource structure
A file organization structure in which any code that handles requests to a resource is stored in a folder with the same name as the resource, regardless of the URL to that resource
Database
A database is defined as an organized collection of data, generally stored and accessed electronically from a computer system.
Companies rely heavily on databases to store the data that powers their applications. Databases store a variety of data, such as usernames, email addresses, and nearly every other type of information that you can imagine.
Databases reside behind a huge percentage of websites. They’re a crucial component of e-commerce systems, telecommunications systems, banking systems, video games, and just about any other software system or electronic device that maintains some amount of persistent information. They’re also reliable, efficient, and scalable, and these properties make them exceptionally useful and convenient.
Hosted database
Also called a managed database, a cloud-computing service in which the end user pays a cloud service provider for access to a database
Relational database management system
RDBMS, a class of programs that can be used to create, update, and administer a relational database
Primary key
A field that uniquely identifies the records in a table
Data definition language
DDL, a subset of SQL commands used to define the tables in the database
SQL
Structured Query Language (SQL) is the database language used to perform operations on a database. You can also use SQL to create a database. SQL uses certain commands, such as CREATE, DROP, and INSERT, to carry out various tasks.
One-to-one relationship
A relationship where a single record in one table is related to a single record in another table, and neither column has duplicate records
One-to-many relationship
A relationship where a single record in one table is related to multiple records in another table
Many-to-many relationship
A relationship where multiple records in one table are related to multiple records in another table
Entity Relationship Diagram
ERD, a diagram that allows database designers to visualize the tables and the relationships between the tables in a database. ERDs are fairly common because they provide an easy way to communicate to others about a database model that’s being built.
Knex Read
Service:
function read(postId) {
return knex(“posts”).select(“*”).where({ post_id: postId }).first();
}
———————————————————————————————–
Controller:
async function postExists(req, res, next) {
const { postId } = req.params;
const post = await service.read(postId); if (post) { res.locals.post = post; return next(); } return next({ status: 404, message: `Post cannot be found.` }); }
function read(req, res) { const { post: data } = res.locals; res.json({ data }); }
module.exports = {
read: [asyncErrorBoundary(postExists), asyncErrorBoundary(read)],
};
————————————————————————————————————
Knex Create
Service:
function create(post) { //your solution here return knex("posts") .insert(post) .returning("*") .then((createdRecords) => createdRecords[0]); } ----------------------------------------------------------------------------- Controller:
async function create(req, res) { // your solution here const data = await service.create(req.body.data); res.status(201).json({data}); } module.exports = { create: asyncErrorBoundary(create),
};
Knex Delete
Service:
function destroy(postId) {
return knex(“posts as p”).where( {“p.post_id”: postId}).del();
}
—————————————————————————————
Controller:
async function postExists(req, res, next) {
const { postId } = req.params;
const post = await service.read(postId); if (post) { res.locals.post = post; return next(); } return next({ status: 404, message: `Post cannot be found.` }); }
async function destroy(req, res) {
await service.delete(res.locals.post.post_id);
res.sendStatus(204);
}
module.exports = {
delete: [asyncErrorBoundary(postExists), asyncErrorBoundary(destroy)],
};
Knex List
Service: function list() { return knex("products").select("*"); } ---------------------------------------------------- Controller: async function list(req, res, next) { const data = await productsService.list(); res.json({ data }); }
Knex Update
Service:
function update(updatedPost) { return knex("posts as p") .select("*") .where({ "p.post_id": updatedPost.post_id}) .update(updatedPost, "*") .then((res) => res[0]) } ------------------------------------------------------------------------ Controller:
async function postExists(req, res, next) {
const { postId } = req.params;
const post = await service.read(postId); if (post) { res.locals.post = post; return next(); } return next({ status: 404, message: `Post cannot be found.` }); }
async function update(req, res) { const updatedPost = { ...req.body.data, post_id: res.locals.post.post_id, } const data = await service.update(updatedPost) res.json({ data }); }
module.exports = {
update: [asyncErrorBoundary(postExists), asyncErrorBoundary(update)],
}