-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_vidly.js
More file actions
69 lines (59 loc) · 2.12 KB
/
index_vidly.js
File metadata and controls
69 lines (59 loc) · 2.12 KB
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
61
62
63
64
65
66
67
68
const express = require('express');
const app = express();
const genre_router = require('./routers/genres');
const home_router = require('./homepage/home');
const morgan = require('morgan');
const mongoose = require('mongoose');
const customers = require('./routers/customers');
const movies = require('./routers/movies');
const rentals = require('./routers/rentals');
const users = require('./routers/users');
const auth = require('./routers/auth');
const config = require('config');
const error = require('./middlewares/error');
const winston = require('winston');
const helmet = require('helmet');
const compression = require('compression');
winston.add(new winston.transports.File({filename: 'logfile.log'}));
process.on('uncaughtException', (ex) => {
winston.error(ex.message, ex);
});
if (!config.get("jwtPrivateKey")) {
console.error("Fatal Error. jwtPrivateKey not defined");
process.exit(1);
} else {
console.log("Starting with secure key ", config.get("jwtPrivateKey"));
}
app.use(express.json());
app.use(morgan('tiny'));
app.use('/', home_router);
app.use('/api/genres', genre_router);
app.use('/api/customers', customers);
app.use('/api/movies', movies);
app.use('/api/rentals', rentals);
app.use('/api/users', users);
app.use('/api/auth', auth);
app.use(helmet());
app.use(compression());
//app.use(error);
connectToMongo();
async function connectToMongo() {
try {
const result = await mongoose.connect(config.get("db"));
console.log("Connected to MongoDB");
} catch (e) {
console.log("Could not connect to MongoDB");
console.log(e.message)
winston.log('error', e.message);
process.exit(); // Terminates the app.listen running loop
}
}
// Reason why program doesnt exit after failure to connect to DB is because
// both connectToMongo() and app.listen are async , and app.listen keeps on
// listening for connections even after async connect to mongo fails.
const PORT = process.env.PORT || 5000;
app.listen(PORT, function () {
//console.log(`Listening to PORT ${PORT}`, `Visit:
// http://localhost:${PORT}`);
winston.info("Server Started");
});