express-allow-methods
Version:
Express middleware for sending HTTP 405 Method Not Allowed response
25 lines • 1.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const methods_1 = __importDefault(require("./methods"));
const hint_impl_1 = __importDefault(require("./hint.impl"));
// TODO: export default function allowMethods(...methods: HttpMethod[]): RequestHandler;
// TODO: export default function allowMethods(config: AllowMethodsConfig): RequestHandler;
function allowMethods(...methods) {
const set = new Set(methods);
for (const method of set)
if (!methods_1.default.has(method))
throw new Error(`Unknown HTTP method encountered: "${method}"` + hint_impl_1.default(method));
const headers = {
Allow: Array.from(set).join(", "),
};
return (req, res, next) => {
if (set.has(req.method))
return next();
res.header(headers).sendStatus(405);
};
}
exports.default = allowMethods;
//# sourceMappingURL=index.js.map