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

34 lines
875 B
JavaScript
Raw Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-05-10 11:04:06 +02:00
// 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;