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// Basic configuration
import { Sequelize } from 'sequelize-typescript';
import config from './config';
// Initialize Sequelize
const sequelize = new Sequelize({
dialect: 'postgres',
...config.postgres,
modelPaths: [__dirname + '/models']
});
// Express
import express from 'express';
// Middleware
import { json } from 'body-parser';
import cors from 'cors';
import { errorHandler, jwtHandler } from './utils/middleware';
// Routers
import answerRouter from './routes/answer';
import authRouter from './routes/auth';
import questionRouter from './routes/question';
import unitRouter from './routes/unit';
import userRouter from './routes/user';
(async () => {
if (typeof config.forceModelSync === 'string') {
await sequelize.model(config.forceModelSync).sync({ force: true });
}
await sequelize.sync();
console.log('models synced!');
// Initialize Express
const app = express();
// Middleware
app.use(cors());
app.use(json());
app.use(jwtHandler);
// Child routers
app.use('/answer', answerRouter);
app.use('/auth', authRouter);
app.use('/question', questionRouter);
app.use('/unit', unitRouter);
app.use('/user', userRouter);
// Static uploads
app.use('/uploads', express.static(__dirname + '/../uploads'));
// Error handler (must be last in the chain!)
app.use(errorHandler);
app.listen(config.port, () => {
console.log(`Server listening on *:${config.port}`);
});
})();