-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
102 lines (86 loc) · 2.72 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
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
'use strict';
require('dotenv').config();
const express = require('express');
const formData = require('express-form-data');
const cors = require('cors');
const morgan = require('morgan');
const passport = require('passport');
const { PORT, CLIENT_ORIGIN } = require('./config.js');
const { dbConnect } = require('./db-mongoose');
const { Account } = require('./models/accounts.js');
const { cronJobRebatch } = require('./cron.js');
// const { dbConnect } = require('./db-knex');
const { router: usersRouter } = require('./routes/users.js');
const { router: authRouter, localStrategy, jwtStrategy } = require('./passport/index.js');
const { router: accountsRouter } = require('./routes/accounts.js');
const { router: incomeRouter } = require('./routes/income.js');
const { router: imageRouter } = require('./routes/images.js');
const app = express();
app.use(express.json());
app.use(formData.parse());
app.use(
morgan(process.env.NODE_ENV === 'production' ? 'common' : 'dev', {
skip: (req, res) => process.env.NODE_ENV === 'test'
})
);
// CORS
app.use(
cors({ origin: CLIENT_ORIGIN })
);
// app.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
// res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE');
// if (req.method === 'OPTIONS') {
// return res.sendStatus(204);
// }
// next();
// });
passport.use(localStrategy);
passport.use(jwtStrategy);
const jwtAuth = passport.authenticate('jwt', { session: false, failWithError: true });
app.use('/api/users', usersRouter);
app.use('/api/auth', authRouter);
app.use('/api/accounts', accountsRouter);
app.use('/api/income', incomeRouter);
app.use('/api/images', imageRouter);
// Custom 404 Not Found route handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Custom Error Handler
app.use((err, req, res, next) => {
if (err.status) {
const errBody = Object.assign({}, err, { message: err.message });
res.status(err.status).json(errBody);
} else {
if (process.env.NODE_ENV === 'development') {
console.error(err);
}
console.error(err);
res.status(500).json({ message: 'Internal Server Error' });
}
});
function runServer(port = PORT) {
const server = app
.listen(port, () => {
console.info(`App listening on port ${server.address().port}`);
})
.on('error', err => {
console.error('Express failed to start');
console.error(err);
});
}
if (require.main === module) {
dbConnect()
.then(() => {
return Account.find({});
})
.then(accounts => {
cronJobRebatch(accounts);
});
runServer();
}
module.exports = { app };