const express = require("express"); const jwtAuthz = require("express-jwt-authz"); const asyncHandler = require("express-async-handler"); const { users } = require("./controllers"); const secure = require("./middleware/secure"); const app = express(); /** * Configure Express.js Middleware */ // Enable CORS app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "*"); res.header("Access-Control-Allow-Headers", "*"); next(); }); // Enable JSON use app.use(express.json()); /** * Routes - Public */ app.options(`*`, (req, res) => { res.status(200).send(); }); app.get(`/test/`, (req, res) => { res.status(200).send("Request received"); }); /** * Routes - Protected */ app.get( "/test-authorized/", secure, jwtAuthz(["read:stuff"]), function (req, res) { res.send("Secured Resource"); } ); /** * Routes - Catch-All */ app.get(`/*`, (req, res) => { res.status(404).json({ error: "Not Found" }); }); /** * Error Handler */ app.use(function (err, req, res, next) { console.error(err); res.status(500).json({ error: `Internal Server Error - "${err.message}"` }); }); module.exports = app;