-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
90 lines (64 loc) · 2.45 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
/**
* @author MrFrenzoid
*/
// Force strict mode.
'use strict';
// We grab our libraries.
require('dotenv').config({ path: './utils/.env' });
const express = require('express');
const boom = require('express-boom');
const logger = require('morgan');
const cors = require('cors');
// We grab the configuration items we need.
const { PORT, BPMBLIMIT, LOGGERLVL, DBHOST, DBNAME, DBUSER, DBDIAL } = require('./config/general.js');
// We grab our database connector and tables generator.
const sequelize = require("./config/database.js");
const { createTablesFromModels, issueRelations } = require("./models/modelsManager.js");
// We grab our helpers
const { jwtMiddleware } = require('./helpers/session')
// We grab our routers.
const Routers = require('./routers/masterRouter.js');
// express app.
const app = express();
// Error standarization.
app.use(boom());
// Body parser configuration
app.use(express.json({ limit: BPMBLIMIT }));
app.use(express.urlencoded({ extended: true }));
// Logger to console.
app.use(logger(LOGGERLVL));
// Enable cors
app.use(cors())
// A default testing endpoint (you can remove this, used to check if the apirest works).
app.get('/api', (_, res) => {
return res.send('API works.');
});
// A default testing endpoint (you can remove this, used to check if the apirest works).
app.get('/api/error', (_, res) => {
let cat = "https://http.cat/502";
res.boom.badGateway(null, { cat });
});
// Our route routers.
// jwtMiddleware is a middleware that we manually made to check for jwt tokens and auto refesh them.
app.use('/api/auth', Routers.auth);
app.use('/api/users', jwtMiddleware, Routers.user);
app.use('/api/items', jwtMiddleware, Routers.item);
// Connect with the database, and start express server :D
try {
// This is dirty, but its the only way to run async code at top level.
(async () => {
await sequelize.authenticate();
console.log(`Sequelize: Successuflly authenticated to: ${DBUSER}@${DBHOST}/${DBNAME}, with dialect: ${DBDIAL}.`);
// We do what we need to finish setting up the database.
// issue relations on our migration.
issueRelations();
// issue transaction, creating tables from models.
createTablesFromModels();
// Start express server.
app.listen(PORT, () => {
console.info(`Express: Server listening on port ${PORT}.`);
});
})();
} catch (error) {
console.error('Unable to connect to the database:', error);
}