Files
cgm-remote-monitor/lib/api/food/index.js
T
Ben West 41fdb8e88f respect per route body-parser configuration
This change allows each route to express policies for interpreting and parsing
the request body.  Before this change uploads to entries or treatments api
would error if they were larger than 100Kb due to the preference being set for
the whole server.  This change removes the global preference in favor of
allowing each route to choose it's own request size limit.

We also refactor usage of body-parser to be more consistent throughout the code
base.  Most routes can use jsonParser, rawPraser, and urlencodedParser provided
by the common wares component.  Anything doing something else should be called
out as such.  For example, treatments, activity, and entries allow uploads up
to 50Mb.  Other v1 endpoints are using the common configuration set to 1Mb.
2021-10-12 22:57:03 -07:00

89 lines
2.6 KiB
JavaScript

'use strict';
var consts = require('../../constants');
function configure (app, wares, ctx) {
var express = require('express'),
api = express.Router( );
// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.rawParser);
// json body types get handled as parsed json
api.use(wares.jsonParser);
// also support url-encoded content-type
api.use(wares.urlencodedParser);
// text body types get handled as raw buffer stream
// shortcut to use extension to specify output content-type
api.use(ctx.authorization.isPermitted('api:food:read'));
// List foods available
api.get('/food/', function(req, res) {
ctx.food.list(function (err, attribute) {
return res.json(attribute);
});
});
api.get('/food/quickpicks', function(req, res) {
ctx.food.listquickpicks(function (err, attribute) {
return res.json(attribute);
});
});
api.get('/food/regular', function(req, res) {
ctx.food.listregular(function (err, attribute) {
return res.json(attribute);
});
});
function config_authed (app, api, wares, ctx) {
// create new record
api.post('/food/', ctx.authorization.isPermitted('api:food:create'), function(req, res) {
var data = req.body;
ctx.food.create(data, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error creating food');
console.log(err);
} else {
res.json(created);
console.log('food created',created);
}
});
});
// update record
api.put('/food/', ctx.authorization.isPermitted('api:food:update'), function(req, res) {
var data = req.body;
ctx.food.save(data, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error saving food');
console.log(err);
} else {
res.json(created);
console.log('food saved');
}
});
});
// delete record
api.delete('/food/:_id', ctx.authorization.isPermitted('api:food:delete'), function(req, res) {
ctx.food.remove(req.params._id, function ( ) {
res.json({ });
});
});
}
if (app.enabled('api')) {
config_authed(app, api, wares, ctx);
}
return api;
}
module.exports = configure;