Browse Source

404,路由优化

dev
npmrun 4 years ago
parent
commit
dff83a9a38
  1. 2
      .env
  2. 2
      package.json
  3. 36
      packages/hapi-router/dist/hapi-router.cjs.js
  4. 2
      packages/hapi-router/dist/hapi-router.cjs.js.map
  5. 3
      packages/hapi-router/package.json
  6. 38
      packages/hapi-router/pnpm-lock.yaml
  7. 1
      packages/hapi-router/rollup.config.js
  8. 13
      packages/hapi-router/src/index.ts
  9. 9
      source/route/index/index.ts
  10. 1
      source/route/route.txt
  11. 31
      source/run.ts
  12. 1
      template/404.ejs

2
.env

@ -1,3 +1,3 @@
NODE_ENV=development
# https://www.bookstack.cn/read/hapijs-zh/2f6997ac6d3fb529.md
KEY = dsRhw1Y5UZqB8SjfClbkrX9PF7yuDMV3JItcW0G4vgpaxONo6mzenHLQET2AiKyPUjjdDko10amjPaba

2
package.json

@ -7,7 +7,7 @@
"scripts": {
"init": "npx ts-node --project ./tsconfig.json -r tsconfig-paths/register source/db/init.ts alter",
"start": "npx ts-node-dev --project ./tsconfig.json -r tsconfig-paths/register ./source/main.ts",
"temp": "nodemon --exec 'npx ts-node-dev -r tsconfig-paths/register ./source/main.ts'"
"dev": "nodemon --exec 'npx ts-node --project ./tsconfig.json -r tsconfig-paths/register ./source/main.ts'"
},
"keywords": [],
"author": "",

36
packages/hapi-router/dist/hapi-router.cjs.js

@ -3,6 +3,30 @@
Object.defineProperty(exports, '__esModule', { value: true });
var Joi = require('joi');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var Joi__namespace = /*#__PURE__*/_interopNamespace(Joi);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
@ -204,8 +228,16 @@ var routePlugin = (function () {
}
}
if (!options_1.validate) {
if (ff.$validate) {
options_1.validate = ff.$validate;
var validateObj = ff.$validate || {};
if (options_1.auth) {
validateObj = Object.assign(validateObj, {
headers: Joi__namespace.object({
Authorization: Joi__namespace.string(),
}).unknown(),
});
}
if (validateObj) {
options_1.validate = validateObj;
}
}
if (ff.$swagger && route.startsWith("/api")) {

2
packages/hapi-router/dist/hapi-router.cjs.js.map

File diff suppressed because one or more lines are too long

3
packages/hapi-router/package.json

@ -34,5 +34,8 @@
"rollup-plugin-typescript2": "^0.27.2",
"tslib": "^2.0.1",
"typescript": "^3.9.7"
},
"dependencies": {
"joi": "^17.4.0"
}
}

38
packages/hapi-router/pnpm-lock.yaml

@ -10,6 +10,7 @@ specifiers:
execa: ^4.0.3
fs-extra: ^9.0.1
ftp-deploy: ^2.3.6
joi: ^17.4.0
lodash: ^4.17.20
rollup: ^2.26.3
rollup-plugin-sourcemaps: ^0.6.2
@ -17,6 +18,9 @@ specifiers:
tslib: ^2.0.1
typescript: ^3.9.7
dependencies:
joi: 17.4.0
devDependencies:
'@rollup/plugin-alias': 3.1.2_rollup@2.51.2
'@rollup/plugin-commonjs': 15.1.0_rollup@2.51.2
@ -36,6 +40,16 @@ devDependencies:
packages:
/@hapi/hoek/9.2.0:
resolution: {integrity: sha512-sqKVVVOe5ivCaXDWivIJYVSaEgdQK9ul7a4Kity5Iw7u9+wBAPbX1RMSnLLmp7O4Vzj0WOWwMAJsTL00xwaNug==}
dev: false
/@hapi/topo/5.0.0:
resolution: {integrity: sha512-tFJlT47db0kMqVm3H4nQYgn6Pwg10GTZHb1pwmSiv1K4ks6drQOtfEF5ZnPjkvC+y4/bUPHK+bc87QvLcL+WMw==}
dependencies:
'@hapi/hoek': 9.2.0
dev: false
/@icetee/ftp/0.3.15:
resolution: {integrity: sha512-RxSa9VjcDWgWCYsaLdZItdCnJj7p4LxggaEk+Y3MP0dHKoxez8ioG07DVekVbZZqccsrL+oPB/N9AzVPxj4blg==}
engines: {node: '>=0.8.0'}
@ -92,6 +106,20 @@ packages:
rollup: 2.51.2
dev: true
/@sideway/address/4.1.2:
resolution: {integrity: sha512-idTz8ibqWFrPU8kMirL0CoPH/A29XOzzAzpyN3zQ4kAWnzmNfFmRaoMNN6VI8ske5M73HZyhIaW4OuSFIdM4oA==}
dependencies:
'@hapi/hoek': 9.2.0
dev: false
/@sideway/formula/3.0.0:
resolution: {integrity: sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==}
dev: false
/@sideway/pinpoint/2.0.0:
resolution: {integrity: sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==}
dev: false
/@types/detect-indent/0.1.30:
resolution: {integrity: sha1-3GgrtBK05lugmOcO2tc7SDP7kQ0=}
dev: true
@ -445,6 +473,16 @@ packages:
resolution: {integrity: sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=}
dev: true
/joi/17.4.0:
resolution: {integrity: sha512-F4WiW2xaV6wc1jxete70Rw4V/VuMd6IN+a5ilZsxG4uYtUXWu2kq9W5P2dz30e7Gmw8RCbY/u/uk+dMPma9tAg==}
dependencies:
'@hapi/hoek': 9.2.0
'@hapi/topo': 5.0.0
'@sideway/address': 4.1.2
'@sideway/formula': 3.0.0
'@sideway/pinpoint': 2.0.0
dev: false
/jsonfile/4.0.0:
resolution: {integrity: sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=}
optionalDependencies:

1
packages/hapi-router/rollup.config.js

@ -20,6 +20,7 @@ export default createConfig();
function createConfig() {
return {
input,
external: ['joi'],
output: createOutput(buildOptions),
plugins: createPlugin(),
};

13
packages/hapi-router/src/index.ts

@ -1,6 +1,7 @@
// @ts-nocheck
import { walkDir, removeIndex, isIndexEnd } from "./util";
import * as Joi from "joi";
const path = require("path")
const fs = require("fs")
@ -60,8 +61,16 @@ class routePlugin {
}
}
if (!options.validate) {
if (ff.$validate) {
options.validate = ff.$validate;
let validateObj = ff.$validate || {};
if(options.auth){
validateObj = Object.assign(validateObj,{
headers: Joi.object({
Authorization: Joi.string(),
}).unknown(), // 注意加上这个
})
}
if (validateObj) {
options.validate = validateObj;
}
}
if (ff.$swagger && route.startsWith("/api")) {

9
source/route/index/index.ts

@ -1,4 +1,4 @@
import { config, method } from "@noderun/hapi-router";
import { config, method, route } from "@noderun/hapi-router";
import UploadFunc from "./_upload";
export default class {
@ -14,7 +14,6 @@ export default class {
allow: "multipart/form-data",
},
})
@method("POST")
async upload(req, h) {
const startTime = new Date().getTime();
@ -25,4 +24,10 @@ export default class {
);
return res;
}
@route("/{path*}")
async 404(req, h) {
// 404页面
return h.view("404.ejs");
}
}

1
source/route/route.txt

@ -4,5 +4,6 @@
/api/user/login 该路由不需要权限
/api/user/del 该路由需要权限
/api/user/userinfo 该路由需要权限
/{path*} 该路由不需要权限
/ 该路由不需要权限
/upload 该路由不需要权限

31
source/run.ts

@ -3,7 +3,8 @@ import plugins from "@/plugins";
import { baseDir } from "@/util";
import validate from "./validate";
const Hapi = require("@hapi/hapi");
const HapiSwagger = require("hapi-swagger");
// const HapiSwagger = require("hapi-swagger");
// const HapiSwagger = require("hapi-swaggered-ui"); // swagger v2版本
const run = async () => {
const server = Hapi.server({
@ -11,20 +12,6 @@ const run = async () => {
host: "localhost",
});
// http://localhost:3000/documentation
const swaggerOptions = {
info: {
title: "Dream 文档",
version: "1.0.0",
},
};
await server.register([
{
plugin: HapiSwagger,
options: swaggerOptions,
},
]);
/**
* jwt
*/
@ -58,6 +45,20 @@ const run = async () => {
path: "template",
});
// http://localhost:3000/documentation
await server.register([
{
plugin: require("hapi-swagger"),
options: {
documentationPath: "/doc",
info: {
title: "Dream 文档",
version: "1.0.0",
},
},
},
]);
await server.start();
console.log("Server running on %s", server.info.uri);
};

1
template/404.ejs

@ -0,0 +1 @@
404
Loading…
Cancel
Save