opt-cc/api/middleware/router-handling.js

34 lines
875 B
JavaScript
Raw Permalink Normal View History

2017-05-10 11:04:06 +02:00
"use strict";
// HTTP status codes by name
const codes = require('../routes/http-codes');
const emptyResponse = (req, res, next) => {
// if anything to send has been added to res.locals.items
if (res.locals.items) {
// then we send it as json and remove it
res.json(res.locals.items);
delete res.locals.items;
} else {
// otherwise we set status to no-content
res.set('Content-Type', 'application/json');
res.status(codes.nocontent).end(); // no content;
}
};
const noHttpMethod = (req, res, next) => {
if (res.locals.items || res.locals.processed) {
next();
} else {
// reply with wrong method code 405
const err = new Error('this method is not allowed at ' + req.originalUrl);
err.status = codes.wrongmethod;
next(err);
}
};
exports.emptyResponse = emptyResponse;
exports.httpMethodNotAllowed = noHttpMethod;