app.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const express = require('express');
  2. const path = require('path');
  3. const logger = require('morgan');
  4. const cors = require('cors');
  5. const helmet = require('helmet');
  6. const apiLimiter = require('./helpers/apiLimiter');
  7. const chatsRouter = require('./routes/chats');
  8. const messagesRouter = require('./routes/messages');
  9. const contactsRouter = require('./routes/contacts');
  10. const userRoute = require('./routes/user');
  11. const app = express();
  12. const DIR_IMAGES = process.env.DIR_IMAGES;
  13. const DIR_AUDIOS = process.env.DIR_AUDIOS;
  14. const DIR_VIDEOS = process.env.DIR_VIDEOS;
  15. const DIR_FILES = process.env.DIR_FILES;
  16. const DIR_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
  17. const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
  18. app.use(helmet());
  19. app.use(logger(formatsLogger));
  20. app.use(cors());
  21. app.use(express.json());
  22. app.use(express.static(path.join(__dirname, DIR_IMAGES)));
  23. app.use(express.static(path.join(__dirname, DIR_AUDIOS)));
  24. app.use(express.static(path.join(__dirname, DIR_VIDEOS)));
  25. app.use(express.static(path.join(__dirname, DIR_FILES)));
  26. app.use(express.static(path.join(__dirname, DIR_NOTIFICATIONS)));
  27. app.use('/api/chats', apiLimiter, chatsRouter);
  28. app.use('/api/messages', apiLimiter, messagesRouter);
  29. app.use('/api/contacts', apiLimiter, contactsRouter);
  30. app.use('/api', apiLimiter, userRoute);
  31. app.use((_req, res) => res.status(404).json({ message: 'Not found' }));
  32. app.use((err, _req, res, _next) =>
  33. res.status(500).json({ message: err.message })
  34. );
  35. module.exports = app;