serve index.html on 404

This commit is contained in:
2019-12-22 20:26:22 +03:00
parent 2db47abd20
commit 7a68132346
2 changed files with 16 additions and 7 deletions

View File

@@ -11,6 +11,7 @@
"@types/koa": "^2.11.0",
"@types/koa-logger": "^3.1.1",
"@types/koa-router": "^7.0.42",
"@types/koa-send": "^4.1.2",
"@types/koa-static": "^4.0.1",
"@types/koa__cors": "^2.2.3",
"@types/lodash": "^4.14.149",
@@ -38,24 +39,25 @@
"tslint-plugin-prettier": "^2.0.1"
},
"dependencies": {
"ts-node": "8.5.4",
"tsconfig-paths": "^3.9.0",
"typescript": "3.7.3",
"concurrently": "^5.0.0",
"cross-env": "^6.0.3",
"@koa/cors": "^3.0.0",
"bcrypt": "^3.0.7",
"concurrently": "^5.0.0",
"cross-env": "^6.0.3",
"jsonwebtoken": "^8.5.1",
"koa": "^2.11.0",
"koa-body": "^4.1.1",
"koa-jwt": "^3.6.0",
"koa-logger": "^3.2.1",
"koa-router": "^7.4.0",
"koa-send": "^5.0.0",
"koa-static": "^5.0.0",
"lodash": "^4.17.15",
"mysql": "^2.17.1",
"reflect-metadata": "^0.1.13",
"typeorm": "0.2.21"
"ts-node": "8.5.4",
"tsconfig-paths": "^3.9.0",
"typeorm": "0.2.21",
"typescript": "3.7.3"
},
"cacheDirectories": [
"frontend/node_modules",

View File

@@ -5,7 +5,9 @@ import * as Koa from "koa";
import * as bodyParser from "koa-body";
import * as jwt from "koa-jwt";
import * as logger from "koa-logger";
import * as send from "koa-send";
import * as serve from "koa-static";
import { config } from "~config";
import { docsRouter } from "~routes/docs";
import { userRouter } from "~routes/users";
@@ -21,11 +23,14 @@ app.use(
passthrough: true,
}),
);
app.use(serve("frontend/dist"));
app.use(async (ctx, next) => {
try {
await next();
const status = ctx.status || 404;
if (status === 404) {
await send(ctx, "frontend/dist/index.html");
}
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
@@ -33,6 +38,8 @@ app.use(async (ctx, next) => {
}
});
app.use(serve("frontend/dist"));
app.use(userRouter.routes()).use(userRouter.allowedMethods());
app.use(docsRouter.routes()).use(docsRouter.allowedMethods());