-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (117 loc) · 4.02 KB
/
server.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
// Authentification
// const jwt = require('express-jwt');
// const jwtAuthz = require('express-jwt-authz');
// const jwksRsa = require('jwks-rsa');
const cors = require('cors');
const morgan = require('morgan');
/**
* Config
*/
const config = require('./config');
const app = express();
const port = config.port;
// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'client/build')));
// if (!config.auth.domain || !config.auth.audience) {
// throw 'Make sure you have auth.domain, and auth.audience in your config file'
// }
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors());
app.use(morgan(`API Request (port ${port}): :method :url :status :response-time ms - :res[content-length]`));
/**
* Winston Logger configuration
*/
const winston = require('winston');
const fs = require('fs');
const env = config.env;
const logDir = config.logDir;
// Create the log directory if it does not exist
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
winston.add(new (require('winston-daily-rotate-file'))({
filename: `${logDir}/log`,
timestamp: tsFormat,
datePattern: 'D-M-Y',
prepend: true,
json: false,
level: env === 'development' ? 'verbose' : 'info',
}));
//
// You can add a separate exception logger by passing it to `.exceptions.handle`
//
// winston.exceptions.handle(
// new winston.transports.File({ filename: `${logDir}/exceptions.log` })
// );
// winston.exitOnError = false;
// Set up mongoose connection
// var mongoose = require('mongoose');
// var dev_db_url = 'mongodb://localhost:27017/pricechecker'
// var mongoDB = process.env.MONGODB_URI || dev_db_url;
// mongoose.connect(mongoDB, { useNewUrlParser: true });
// mongoose.Promise = global.Promise;
// var db = mongoose.connection;
// db.on('error', console.error.bind(console, 'MongoDB connection error:'));
/**
* Connect to MongoDB via Mongoose
*/
var mongoose = require('mongoose')
const opts = {
promiseLibrary: global.Promise,
auto_reconnect: true,
autoIndex: true,
useNewUrlParser: true
}
mongoose.Promise = opts.promiseLibrary
mongoose.connect(config.db.uri, opts)
const db = mongoose.connection
db.on('error', (err) => {
console.error(err);
if (err.message.code === 'ETIMEDOUT') {
winston.error('Db connection error. Reconnect is required.', err)
mongoose.connect(config.db.uri, opts)
}
})
db.once('open', () => {
console.log('Opened fine');
app.use(require('./routes/users'));
app.use(require('./routes/crawlers'));
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
//Start crawling
const crawlersManager = require('./helpers/crawlersManager');
crawlersManager.startAll();
app.listen(port, () => console.log(`Server is listening on port ${port}`));
winston.info(`Server is listening on port ${port}`);
})
// Authentification to API using JWT
// Commented out for time being.
// const checkJwt = jwt({
// // Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.
// secret: jwksRsa.expressJwtSecret({
// cache: true,
// rateLimit: true,
// jwksRequestsPerMinute: 5,
// jwksUri: `https://${config.auth.domain}/.well-known/jwks.json`
// }),
// // Validate the audience and the issuer.
// audience: config.auth.audience,
// issuer: `https://${config.auth.domain}/`,
// algorithms: ['RS256']
// });
// const checkScopes = jwtAuthz([ 'read:messages' ]);
// app.get('/api/hello', checkJwt, checkScopes, (req, res) => {
// res.send({ express: 'Hello From Express' });
// });
//not to cancel dependent thread tasks
// process.on('uncaughtException', err => console.log(err));