app.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var createError = require('http-errors');
  2. var express = require('express');
  3. var path = require('path');
  4. var cookieParser = require('cookie-parser');
  5. var logger = require('morgan');
  6. var dbConfig = require('./db.js');
  7. var Klapi = require('klapi')(dbConfig);
  8. var app = express();
  9. var router = express.Router();
  10. const cors = require('cors');
  11. // view engine setup
  12. app.use(cors());
  13. app.use(logger('dev'));
  14. app.use(express.json());
  15. app.use(express.urlencoded({ extended: false }));
  16. app.use(cookieParser());
  17. app.use(express.static(path.join(__dirname, 'public')));
  18. Klapi.Systems.RepositorySystem.setDB(dbConfig);
  19. Klapi = Klapi.minimal(router);
  20. Klapi.ModuleSystem.loadModules('./modules');
  21. Klapi.ModuleSystem.run();
  22. let prefix = process.env.NODE_ENV === 'production' ? '/api' : '';
  23. app.use(prefix, router);
  24. // catch 404 and forward to error handler
  25. app.use(function(req, res, next) {
  26. next(createError(404));
  27. });
  28. // error handler
  29. app.use(function(err, req, res, next) {
  30. // set locals, only providing error in development
  31. res.locals.message = err.message;
  32. res.locals.error = req.app.get('env') === 'development' ? err : {};
  33. // console.log(res.locals.error);
  34. // render the error page
  35. res.status(err.status || 500);
  36. res.send('error');
  37. });
  38. module.exports = app;