app.js 983 B

12345678910111213141516171819202122232425262728
  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 chatsRouter = require('./routes/chats');
  7. const messagesRouter = require('./routes/messages');
  8. const contactsRouter = require('./routes/contacts');
  9. const userRoute = require('./routes/user');
  10. const app = express();
  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;