-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.68 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const compression = require('compression');
const cors = require('cors');
const dotenv = require('dotenv');
const express = require('express');
require('express-async-errors');
const helmet = require('helmet');
const path = require('path');
const morgan = require('morgan');
process.on('uncaughtException', (err) => {
console.log('Uncaught exception:', err);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled rejection at:', promise, '\nReason:', reason);
process.exit(1);
});
// Load .env file contents to process.env
const { error } = dotenv.config({ path: path.join(__dirname, '.env') });
if (error) {
throw error;
}
const NODE_ENV = process.env.NODE_ENV || 'development';
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan(NODE_ENV === 'production' ? 'combined' : 'dev'));
app.use(cors({ credentials: true }));
// Setup production-only middlewares
if (NODE_ENV === 'production') {
app.use(helmet());
app.use(compression());
}
app.use('/api/auth', require('./routes/auth'));
app.use('/api/customers', require('./routes/customers'));
app.use('/api/genres', require('./routes/genres'));
app.use('/api/movies', require('./routes/movies'));
app.use('/api/rentals', require('./routes/rentals'));
app.use('/api/returns', require('./routes/returns'));
app.use('/api/users', require('./routes/users'));
app.all('*', (req, res) => {
res.sendStatus(405);
});
app.use(require('./middlewares/error'));
console.log(`Starting in ${NODE_ENV} mode`);
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
app.listen(PORT, () => {
console.log(`Listening on port :${PORT}`);
});