Mocks and Intrusions - abukhalil-LTUC-ASAC/amman-401d4 GitHub Wiki
API mocks and Middleware usage
Your daily development friends, could be more handy that you first thought, mockers and intruders do have their own usefulness after all.
Middleware
What we would need to handle mid request, depending on many factors is what a Middleware does for a living. There are multiple ways Middleware operates, many of these are what you might be familiar is:
-
Global such inside of
server.js
app.use(express.json());
which allows for parsing of incoming data into a JSON object,logger.js
to handle logging of all requests and responses, they execute to the scope of the whole file. Other functions are also global can be called anywhere such asapp.use('*', notFoundHandler);
which invalidates all other routes after this point. -
Local or more specialized Middleware are similar to
app.get('/route', middleware, (req,res) => {});
and you would define middlewares as
function middleware(req, res, next) {
do-logic;
next(); //moves to the next middleware
}
Passing in arguments into route middlewares requires wrapping the original function as a normal one, eg: app.get('/route', middleware(n), (req,res) => {});
so that you could pass in the variable and return a proper middleware with a next() method.
function middleware(n){
return (req, res, next) => {
do-logic;
next(); //moves to the next middleware
}
}
API
Mocks of such are made to ease front-end developers if they knew the shape of incoming objects but have no say in the backend project, so instead of recreating the full experience, they would use mock tools such as:
- Mirage JS
- Jest, yes JEST!!
- Cypress
- json-server
A new request of PUT and PATCH is going to be introduced, PUT does a full replacement of the resource, while PATCH changes part of the resource instead. New file would be introduced, the XML and its SOAP protocol that handles this type of files. Swagger is a major tool to use to document the API in an automatic way, including design and testing of such API services.