-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (34 loc) · 1.08 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const cors = require('cors');
const config = require('./utils/config');
const logger = require('./utils/logger');
const middleware = require('./utils/middleware');
const usersRouter = require('./controllers/users');
const booksRouter = require('./controllers/books');
const loginRouter = require('./controllers/login');
const testingRouter = require('./controllers/testing');
const app = express();
mongoose
.connect(config.MONGO_URI)
.then(() => {
logger.info('Connected to database');
})
.catch((error) => {
logger.error(error);
});
app.use(cors());
app.use(express.static('dist'));
app.use(express.json());
app.use(morgan('tiny'));
app.use(middleware.tokenExtractor);
if (process.env.NODE_ENV === 'test') {
app.use('/api/testing', testingRouter);
}
app.use('/api/login', loginRouter);
app.use('/api/users', usersRouter);
app.use('/api/books', middleware.userExtractor, booksRouter);
app.use(middleware.errorHandler);
module.exports = app;