MERN Flashcards
What is MERN?
A development stack. It stands for MongoDB, Express, Node.js, and React.js
What are the main HTTP requests to a REST API?
GET, POST, PUT, PATCH, DELETE
Difference between REST and graphQL API?
REST have multiple endpoints, graphQL typically have only one. graphQL uses query paramaters to differientiate between different kinds of request
Two different ways of connecting React and Node.
Combined: runs on the same server and uses a single domain name.
Decoupled: runs on seperate servers each with their own domain names. Is more responsive than Combined app.
Rendered: The server renders its own html.
API
Application programming interface
SPA
Single Page Application
Why don’t we connect React directly to MongoDB?
It is a security concern, because doing so would expose sensitive information to the front end which anyone would be able to read.
How many servers are typically involved in a MERN application
Three servers. One that runs REACT, one that runs the backend API (NODE.js, EXPRESS) and one that runs the MongoDB data base.
How to make a post request to the backend from the frontend?
const response = await fetch(‘http//:localhost:3000/api/users/signup’,{
method:’POST’,
headers:{
‘Content-Type’:’application/json’,
},
body:JSON.stringify({
name:formState.inputs.name.value,
email:formState.inputs.email.value,
password:formState.inputs.password.value
})
const responseData = await response.json()
What is a CORS error?
CORS cross origin resource sharing, is a security concept enforced by the browser
The same-origin policy is very restrictive. Under this policy, a document (i.e., like a web page) hosted on server A can only interact with other documents that are also on server A. In short, the same-origin policy enforces that documents that interact with each other have the same origin.
An origin is made up of the following three parts: the protocol, host, and port number.
Resources on a server should only be shared or accessed by requests coming from the same server or origin.
How to set CORS headers middleware on the Express server?
//Needs to be set before the routes are defined.
server.use((req,res,next)=>{ //Which domains should have access res.setHeader( 'Access-Control-Allow-Origin', '*' ); //Which Headers requests may have res.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization' ); //Which methods requests may include res.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE' ); next(); });