Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: upgrade router_test.ts
  • Loading branch information
lengfangbing committed Jun 23, 2020
1 parent e43f1f0 commit 4da1fe5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
61 changes: 26 additions & 35 deletions application_test.ts
Expand Up @@ -69,15 +69,12 @@ export class Application {
return this;
}

#setRoutes = async (routes: RoutesConfig[], cwd: string) => {
#setRoutes = async (routes: RoutesConfig[]) => {
for(let i = 0; i < routes.length; i++){
const value = routes[i];
const method: ReqMethod = value.method.toLowerCase() as ReqMethod;
const { url, func, middleware = [] } = value;
const handler =
typeof func === 'string'
? (await import(join(cwd, func))).default
: func;
const handler = func;
this.#add(method, {url, middleware, handler});
}
}
Expand Down Expand Up @@ -195,23 +192,17 @@ export class Application {
}

#readConfig = async (config?: any) => {
const cwd = Deno.cwd();
if(!config){
try{
config = (await import (join(cwd, 'min.config.ts'))).default;
}catch(e){
throw Error ('no such file named min.config.ts, please check the name or provide a min.config.js by yourself ');
}
}
// config.constructor === undefined => config = await import('./min.config.ts')
config = config.constructor ? config : config.default;
const { server, routes, cors: corsConfig, assets: assetsConfig = '' } = config;
// set server config
appConfig.server = server.addr
? {...server, ...parseAddress(server.addr)}
: server;
routes?.length
&& await this.#setRoutes(routes, cwd);
&& await this.#setRoutes(routes);
corsConfig
&& this.use(cors(corsConfig));
&& this.use(cors(corsConfig));
this.use(assets(assetsConfig));
}

Expand Down Expand Up @@ -246,27 +237,27 @@ export class Application {
await this.#listen();
}

async start(config?: any){
async start(config: any){
await this.#readConfig(config);
await this.#listen();
}
}
const a = new Application();
// fetch url /name/100/v1/detail
a.get('/name/:id/:version/detail', (req: Req, res: Res) => {
assertEquals({id: '100', version: 'v1'}, req.params);
res.body = req.params;
});
a.get('/test', (req: Req, res: Res) => {
res.body = [1,2,3,4];
});
// fetch url /name/110/fangbing/ting/v1
a.get('/name/:id/:name/ting/:version', (req: Req, res: Res) => {
assertEquals({id: '110', name: 'fangbing', version: 'v1'}, req.params);
res.body = {
params: req.params,
query: req.query,
url: req.url
}
});
await a.listen('http://127.0.0.1:8000');
// const a = new Application();
// // fetch url /name/100/v1/detail
// a.get('/name/:id/:version/detail', (req: Req, res: Res) => {
// assertEquals({id: '100', version: 'v1'}, req.params);
// res.body = req.params;
// });
// a.get('/test', (req: Req, res: Res) => {
// res.body = [1,2,3,4];
// });
// // fetch url /name/110/fangbing/ting/v1
// a.get('/name/:id/:name/ting/:version', (req: Req, res: Res) => {
// assertEquals({id: '110', name: 'fangbing', version: 'v1'}, req.params);
// res.body = {
// params: req.params,
// query: req.query,
// url: req.url
// }
// });
// await a.listen('http://127.0.0.1:8000');
21 changes: 21 additions & 0 deletions mod_test.ts
@@ -0,0 +1,21 @@
export {
Application
} from './application_test.ts';

export {
cors
} from './cors.ts';

export {
MinConfig,
Req,
Res
} from './model.ts';

export{
Request
} from './request.ts';

export{
Response
} from './response.ts';
25 changes: 7 additions & 18 deletions router_test.ts
Expand Up @@ -45,7 +45,7 @@ export class Router {
}

#findLoop = (map: Record<string, NewRoute | null>, urls: string[]): SingleRoute | null => {
// 回退查找的数组
// 回溯查找的数组
let _map: Function[] = [];
// 当前查找到的静态处理Route
let routeVal: NewRoute | null = null;
Expand Down Expand Up @@ -140,12 +140,12 @@ export class Router {
const urls = splitPath(url);
if (!urls.length) throw new Error('router path is invalid, use /path/... instead');
let p: NewRoute | null = null;
let params: {[key: string]: string } | null = null;
let params: {[key: string]: string } = {};
urls.forEach((value: string | { paramsName: string }, index: number) => {
// 静态路由
if (typeof value === 'string') {
// 如果p代表了有值了, 就代表funcMap有匹配项了
if (p) {
if (p !== null) {
// 如果p有next, 表示p有下一节点, 接下来判断是否有这个value节点
if (p.next) {
if (p.next[value]) {
Expand Down Expand Up @@ -200,11 +200,7 @@ export class Router {
}
p = funcMap[''];
}
if (params) {
params[index] = value.paramsName;
} else {
params = {[index]: value.paramsName};
}
params[index] = value.paramsName;
} else {
if (p.next) {
if (p.next['']) {
Expand All @@ -218,11 +214,7 @@ export class Router {
}
p = p.next[''];
}
if (params) {
params[index] = value.paramsName;
} else {
params = {[index]: value.paramsName};
}
params[index] = value.paramsName;
} else {
p.next = {
'': {
Expand All @@ -233,11 +225,7 @@ export class Router {
}
}
p = p.next[''];
if (params) {
params[index] = value.paramsName;
} else {
params = {[index]: value.paramsName};
}
params[index] = value.paramsName;
}
}
}
Expand All @@ -254,6 +242,7 @@ export class Router {
return this.#tree;
}
}
console.log('you imported test module');
const router = new Router();
const path1 = '/name/:id/:version/detail';
const url = '/name/2016207235/v1/detail';
Expand Down

0 comments on commit 4da1fe5

Please sign in to comment.