app.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. /*
  3. *
  4. * Copyright 2016-2017 Red Hat, Inc, and individual contributors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. const logger = require('./logger.js');
  20. const path = require('path');
  21. const express = require('express');
  22. const bodyParser = require('body-parser');
  23. const app = express();
  24. const db = require('./lib/db');
  25. const fruits = require('./lib/routes/fruits');
  26. app.use(bodyParser.json());
  27. app.use((error, request, response, next) => {
  28. if (request.body === '' || (error instanceof SyntaxError && error.type === 'entity.parse.failed')) {
  29. response.status(415);
  30. return response.send('Invalid payload!');
  31. }
  32. next();
  33. });
  34. app.use(bodyParser.urlencoded({ extended: false }));
  35. app.use(express.static(path.join(__dirname, 'public')));
  36. app.use('/api', fruits);
  37. // Add a health check
  38. app.use('/ready', (request, response) => {
  39. return response.sendStatus(200);
  40. });
  41. app.use('/live', (request, response) => {
  42. return response.sendStatus(200);
  43. });
  44. db.init().then(() => {
  45. logger.info('Database init\'d');
  46. }).catch(error => {
  47. logger.error(error);
  48. });
  49. module.exports = app;