-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (41 loc) · 1.5 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
const HTTPStatus = require("http-status");
const Router = require("@koa/router");
const { NotFoundError } = require("@salsita/errors");
const errorMiddleware = require("@salsita/koa-error-middleware");
const config = require("../config");
const log = require("../helpers/log");
const pe = require("../helpers/prettyError");
const v1 = require("./v1");
const { apiBase } = config;
const apiRouter = new Router({
prefix: apiBase,
});
const formatUserMessage = (err) =>
process.env.NODE_ENV === "production"
? err.message || "Ooops something went wrong"
: pe(err, false, false);
apiRouter.use(errorMiddleware(log, formatUserMessage));
apiRouter.use("/v1", v1.routes(), v1.allowedMethods());
apiRouter.all("(.*)", () => {
throw new NotFoundError(HTTPStatus[HTTPStatus.NOT_FOUND]);
});
// module.exports = apiRouter;
// "HACK" for auth demo below
/* eslint-disable import/order,import/no-extraneous-dependencies */
const distDir = "./client-auth/build";
const authPrefix = "/auth";
const serve = require("koa-static")(distDir);
const send = require("koa-send");
const authRouter = new Router({
prefix: authPrefix,
});
authRouter.use(async (ctx, next) => {
ctx.path = ctx.path.replace(authPrefix, "") || "/";
await serve(ctx, next);
});
authRouter.use("(.*)", (ctx) => send(ctx, `${distDir}/index.html`));
authRouter.all("(.*)", () => {});
const router = new Router();
router.use(apiRouter.routes(), apiRouter.allowedMethods());
router.use(authRouter.routes(), authRouter.allowedMethods());
module.exports = router;