源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

NodeJS仿WebApi路由示例

  • 时间:2021-08-29 04:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:NodeJS仿WebApi路由示例
用过WebApi或Asp.net MVC的都知道微软的路由设计得非常好,十分方便,也十分灵活。虽然个人看来是有的太灵活了,team内的不同开发很容易使用不同的路由方式而显得有点混乱。 不过这不是重点,我在做Node项目的时候就觉得不停的用[code]use(...)[/code]来指定路由路径很烦人,所以用[code]Typescript[/code]写了这个基于[code]Koa[/code]和[code]Koa-router[/code]的路由插件,可以简单实现一些类似WebApi的路由功能。 [b]目标是和WebApi一样:[/b] 1.加入的controller会自动加入路由。 2.也可以通过path()手动指定路由。 3.可以定义http method, 如[code]GET[/code]或[code]POST[/code]等。 4.Api的参数可以指定url里的query param、path param以及body等。 包已经上传到npm中,npm install webapi-router 安装,可以先看看效果: [b]第一步,先设置controllers的目录和url的固定前缀[/b] 所有的controller都在这目录下,这样会根据物理路径自动算出路由。 url的固定前缀就是host和路由之间的,比如[code]localhost/api/v2/user/name[/code],[code]api/v2[/code]就是这个固定前缀。
import { WebApiRouter } from 'webapi-router';

app.use(new WebApiRouter().router('sample/controllers', 'api'));
[b]第二步是controller都继承自[code]BaseController[/code][/b]
export class TestController extends BaseController
{

}
[b]第三步给controller的方法加上装饰器[/b]
@POST('/user/:name')
postWithPathParam(@PathParam('name') name: string, @QueryParam('id') id: string, @BodyParam body: any) {
  console.info(`TestController - post with name: ${name}, body: ${JSON.stringify(body)}`);
  return 'ok';
}
[code]@POST[/code]里的参数是可选的,空的话会用这个controller的物理路径做为路由地址。 [code]:name[/code]是路径里的变量,比如[code] /user/brook[/code], [code]:name[/code]就是[code]brook[/code],可以在方法的参数里用@PathParam得到 [code]@QueryParam[/code]可以得到[code]url[/code]里[code]?[/code]后的参数 [code]@BodyParam[/code]可以得到[code]Post[/code]上来的[code]body[/code] 是不是有点WebApi的意思了。 [b]现在具体看看是怎么实现的[/b] 实现过程其实很简单,从上面的目标入手,首先得到controllers的物理路径,然后还要得到被装饰器装饰的方法以及它的参数。 装饰器的目的在于要得到是[code]Get[/code]还是[code]Post[/code]等,还有就是指定的[code]Path[/code],最后就是把node request里的数据赋值给方法的参数。 核心代码: [b]得到物理路径[/b]
initRouterForControllers() {
  //找出指定目录下的所有继承自BaseController的.js文件
  let files = FileUtil.getFiles(this.controllerFolder);

  files.forEach(file => {
    let exportClass = require(file).default;

    if(this.isAvalidController(exportClass)){
      this.setRouterForClass(exportClass, file);
    }
  });
}
从物理路径转成路由
private buildControllerRouter(file: string){

  let relativeFile = Path.relative(Path.join(FileUtil.getApiDir(), this.controllerFolder), file);
  let controllerPath = '/' + relativeFile.replace(/\\/g, '/').replace('.js','').toLowerCase();

  if(controllerPath.endsWith('controller'))
    controllerPath = controllerPath.substring(0, controllerPath.length - 10);

  return controllerPath;
}
[b]装饰器的实现[/b] 装饰器需要引入[code]reflect-metadata库[/code] 先看看方法的装饰器,[code]@GET[/code],[code]@POST[/code]之类的,实现方法是给装饰的方法加一个属性[code]Router[/code],[code]Router[/code]是个[code]Symbol[/code],确保唯一。 然后分析装饰的功能存到这个属性中,比如[code]Method[/code],[code]Path[/code]等。
export function GET(path?: string) {
  return (target: BaseController, name: string) => setMethodDecorator(target, name, 'GET', path);
} 

function setMethodDecorator(target: BaseController, name: string, method: string, path?: string){
  target[Router] = target[Router] || {};
  target[Router][name] = target[Router][name] || {};
  target[Router][name].method = method;
  target[Router][name].path = path;
}

另外还有参数装饰器,用来给参数赋上[code]request[/code]里的值,如[code]body[/code],[code]param[/code]等。
export function BodyParam(target: BaseController, name: string, index: number) {
  setParamDecorator(target, name, index, { name: "", type: ParamType.Body });
}

function setParamDecorator(target: BaseController, name: string, index: number, value: {name: string, type: ParamType}) {
  let paramTypes = Reflect.getMetadata("design:paramtypes", target, name);
  target[Router] = target[Router] || {};
  target[Router][name] = target[Router][name] || {};
  target[Router][name].params = target[Router][name].params || [];
  target[Router][name].params[index] = { type: paramTypes[index], name: value.name, paramType: value.type };
}
这样装饰的数据就存到对象的Router属性上,后面构建路由时就可以用了。 [b]绑定路由到[code]Koa-router[/code]上[/b] 上面从物理路径得到了路由,但是是以装饰里的参数路径优先,所以先看看刚在存在原型里的[code]Router[/code]属性里有没有[code]Path[/code],有的话就用这个作为路由,没有[code]Path[/code]就用物理路由。
private setRouterForClass(exportClass: any, file: string) { 

  let controllerRouterPath = this.buildControllerRouter(file);
  let controller = new exportClass();

  for(let funcName in exportClass.prototype[Router]){
    let method = exportClass.prototype[Router][funcName].method.toLowerCase();
    let path = exportClass.prototype[Router][funcName].path;

    this.setRouterForFunction(method, controller, funcName, path ? `/${this.urlPrefix}${path}` : `/${this.urlPrefix}${controllerRouterPath}/${funcName}`);
  }
}
给controller里的方法参数赋上值并绑定路由到[code]KoaRouter[/code]
private setRouterForFunction(method: string, controller: any, funcName: string, routerPath: string){
  this.koaRouter[method](routerPath, async (ctx, next) => { await this.execApi(ctx, next, controller, funcName) });
}

private async execApi(ctx: Koa.Context, next: Function, controller: any, funcName: string) : Promise<void> { //这里就是执行controller的api方法了
  try
  {
    ctx.body = await controller[funcName](...this.buildFuncParams(ctx, controller, controller[funcName]));
  }
  catch(err)
  {
    console.error(err);
    next(); 
  }
}

private buildFuncParams(ctx: any, controller: any, func: Function) { //把参数具体的值收集起来
  let paramsInfo = controller[Router][func.name].params;
  let params = [];
  if(paramsInfo)
  {
    for(let i = 0; i < paramsInfo.length; i++) {
      if(paramsInfo[i]){
        params.push(paramsInfo[i].type(this.getParam(ctx, paramsInfo[i].paramType, paramsInfo[i].name)));
      } else {
        params.push(ctx);
      }
    }
  }
  return params;
}

private getParam(ctx: any, paramType: ParamType, name: string){ // 从ctx里把需要的参数拿出来
  switch(paramType){
    case ParamType.Query:
      return ctx.query[name];
    case ParamType.Path:
      return ctx.params[name];
    case ParamType.Body:
      return ctx.request.body;
    default:
      console.error('does not support this param type');
  }
}

这样就完成了简单版的类似WebApi的路由. 源码下载[url=http://xiazai.jb51.net/201702/yuanma/webapi-router_jb51.rar]webapi-router_jb51.rar[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部