123456789101112131415161718192021222324252627282930313233343536 |
- const express = require('express');
- const path = require('path');
- const logger = require('morgan');
- const cors = require('cors');
- const helmet = require('helmet');
- const apiLimiter = require('./helpers/apiLimiter');
- const chatsRouter = require('./routes/chats');
- const messagesRouter = require('./routes/messages');
- const contactsRouter = require('./routes/contacts');
- const userRoute = require('./routes/user');
- const app = express();
- const DIR_IMAGES = process.env.DIR_IMAGES;
- const DIR_AUDIOS = process.env.DIR_AUDIOS;
- const DIR_VIDEOS = process.env.DIR_VIDEOS;
- const DIR_FILES = process.env.DIR_FILES;
- const DIR_NOTIFICATIONS = process.env.DIR_NOTIFICATIONS;
- const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
- app.use(helmet());
- app.use(logger(formatsLogger));
- app.use(cors());
- app.use(express.json());
- app.use(express.static(path.join(__dirname, DIR_IMAGES)));
- app.use(express.static(path.join(__dirname, DIR_AUDIOS)));
- app.use(express.static(path.join(__dirname, DIR_VIDEOS)));
- app.use(express.static(path.join(__dirname, DIR_FILES)));
- app.use(express.static(path.join(__dirname, DIR_NOTIFICATIONS)));
- app.use('/api/chats', apiLimiter, chatsRouter);
- app.use('/api/messages', apiLimiter, messagesRouter);
- app.use('/api/contacts', apiLimiter, contactsRouter);
- app.use('/api', apiLimiter, userRoute);
- app.use((_req, res) => res.status(404).json({ message: 'Not found' }));
- app.use((err, _req, res, _next) =>
- res.status(500).json({ message: err.message })
- );
- module.exports = app;
|