test middleware

This commit is contained in:
Karol Wypchlo 2021-02-05 11:54:05 +01:00
parent a40a4f2ecb
commit 28ba0eb3d3
1 changed files with 18 additions and 2 deletions

View File

@ -17,10 +17,26 @@ const middleware = jwt({
algorithms: ["RS256"], algorithms: ["RS256"],
}); });
export default middleware((req, res) => { // Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
export default (req, res) => {
await runMiddleware(req, res, middleware)
console.log(Object.keys(req)); console.log(Object.keys(req));
console.log(req); console.log(req);
console.log(JSON.stringify(req)); console.log(JSON.stringify(req));
res.statusCode = 200; res.statusCode = 200;
res.json({ name: "John Doe" }); res.json({ name: "John Doe" });
}); };