-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (54 loc) · 1.52 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
const config = require('./config');
const Koa = require('koa');
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const cors = require('@koa/cors');
const { ServerError, NotFoundError } = require('./src/helpers/error');
const authRouter = require('./src/api/routes/auth');
const userRouter = require('./src/api/routes/users');
const eventRouter = require('./src/api/routes/events');
const eventTypeRouter = require('./src/api/routes/eventTypes');
const app = new Koa();
const mainRouter = new Router({
prefix: '/api',
});
// error handling
app.use(async (ctx, next) => {
try {
await next();
const status = ctx.status;
if (status === 404) {
throw new NotFoundError();
}
} catch (err) {
if (err instanceof ServerError) {
console.log(err.stack);
}
ctx.status = err.status || 500;
ctx.body = {
status: err.status,
message: err.message || 'Internal Server Error',
};
ctx.app.emit('error', err, ctx);
}
});
app.use(logger());
const routers = {
'/auth': authRouter,
'/users': userRouter,
'/events': eventRouter,
'/event-types': eventTypeRouter,
};
Object.entries(routers).forEach(([path, router]) => {
mainRouter.use(path, router.routes(), router.allowedMethods());
});
// utils
app.use(cors());
app.use(bodyParser());
// routes
app.use(mainRouter.allowedMethods());
app.use(mainRouter.routes());
module.exports = app.listen(config.port, () =>
console.log(`App started on port ${config.port}`)
);