app.js 983 B

12345678910111213141516171819202122232425262728
  1. const express = require('express');
  2. const app = express();
  3. const path = require('path');
  4. const logger = require('morgan');
  5. const cors = require('cors');
  6. const helmet = require('helmet');
  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 DIR_STATIC = process.env.DIR_STATIC;
  12. const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
  13. app.use(helmet());
  14. app.use(logger(formatsLogger));
  15. app.use(cors());
  16. app.use(express.json());
  17. app.use(express.static(path.join(__dirname, DIR_STATIC)));
  18. app.use('/api/chats', chatsRouter);
  19. app.use('/api/messages', messagesRouter);
  20. app.use('/api/contacts', contactsRouter);
  21. app.use('/api', userRoute);
  22. app.use((_req, res) => res.status(404).json({ message: 'Not found' }));
  23. app.use((err, _req, res, _next) =>
  24. res.status(500).json({ message: err.message })
  25. );
  26. module.exports = app;