Angular4+NodeJs+MySQL 入门-03 后台接口定义

这篇里是如何定义接口,我们一般访问接口如:post请求调用http://127.0.0.1:11000/webapi/userinfo/user 这个接口,成功返回用户信息,如果失败要返回失败原因等。

首先分析一下 /webapi/userinfo/login 接口。从这里可以看出 webapi是一个类,userinfo也是一个类,user是一个方法。再接合post、delete、put、get请求可以得到四个方法了,这样就可以实现增、删、改、查的功能。

接下我们就先创建一个webapi.js文件,里面的内容如下

const UserInfo = require('./UserInfo');
module.exports = {
  userinfo: new UserInfo(),
};

再就是创建UserInfo.js,里面的内容:

/**
 * 用户信息类
 * 
 * @class UserInfo
 */
class UserInfo {
  constructor(DbHelper, Utility) {
    this.DbHelper = DbHelper;
    this.Utility = Utility;
  }

  post_login(request, response, options) {

  }

  get_user(request, response, options) {
    response.Send({ msg: '这是一个get请求', options });
  }

  post_user(request, response, options) {
    response.Send({ msg: '这是一个 post 请求', options });
  }

  delete_user(request, response, options) {
    response.Send({ msg: '这是一个 delete 请求', options });
  }

  put_user(request, response, options) {
    response.Send({ msg: '这是一个 put 请求', options });
  }
}
module.exports = UserInfo;

光这样写还是不行的啦,还要修改在上上篇中router里的代码,要不是调用不了接口的。
我们得还要建一个 index.js文件

// 这个是之前引用进来,这个就是/webapi部分
const webapi = require('./webapi');
// const order = require('./orderapi'); http://xxx/orderapi/list
// const car = require('./carapi');     http://xxx/car/goods 
// const DealBusiness = require('./DealBusiness');
// ...
// 
module.exports = {
  webapi, 
  // order,car ,  DealBusiness: new DealBusiness() ,...
}

创建完这个文件好了后,在router里从require(index.js)文件了。通过匹配url路径中的匹配到定义好的接口。找了就调用相应的接口,没有找到的就向返回400等信息
这里写图片描述

定好了,就可以先用postman来试一下接口定义是否可以用了。下图是调用get请求的情况。
这里写图片描述

下面这张图是调用post请求返回的信息
这里写图片描述

这样接口调用基本就搞定啦。

这里完整把router.js里router类在这里放一下

class routes {
  constructor(req, res) {
    this.ApiInfo = api;
    this.res = res;
    this.req = req;
  }

  initHeader() {
    this.res.setHeader("Content-Type", "application/json;charset=utf-8");
    this.res.setHeader("Access-Control-Allow-Origin", "*");
    this.res.setHeader("access-control-allow-headers", "x-pingother, origin, x-requested-with, content-type, accept, xiaotuni,systemdate");
    this.res.setHeader("access-control-allow-methods", "GET, POST, PUT, DELETE, OPTIONS");
    this.res.setHeader("Access-Control-Expose-Headers", "date, token,systemdate");
    this.res.setHeader('systemdate', new Date().getTime());
    const { method } = this.req;
    if (method && method === 'OPTIONS') {
      this.res.end();
      return;
    }

    this.processRequestMethod(method);
  }

  processRequestMethod(method) {
    const PathInfo = path.parse(this.req.url);
    if (!this.judgeIsCallApi(PathInfo)) {
      return;
    }
    this.Method = method.toLocaleLowerCase();
    this.parseUrlParams();

    this.__ProcessApi(PathInfo);
  }

  __ProcessApi(PathInfo) {
    const methodInfo = { pathname: this.UrlInfo.pathname, method: this.Method };
    // 以utf-8的形式接受body数据
    this.req.setEncoding('utf8');
    let __ReData = "";
    // 这里接受用户调用接口时,向body发送的数据
    this.req.on('data', (data) => {
      __ReData += data;
    });
    const __self = this;
    this.req.on('end', () => {      // 监听数据接受完后事件。
      // 查询用户定义好的接口。
      const { func, ctrl } = __self.__FindMethod(PathInfo) || {};
      const data = __ReData && __ReData !== '' ? JSON.parse(__ReData) : {};
      if (func) {
        func.apply(ctrl, [__self.req, __self.res, { params: __self.QueryParams, data }]);
        return;
      }
      const _db = new DbHelper(); // 实例化一个数据库操作类
      __self.ApiInfo.DealBusiness.Process(_db, __self.req, __self.res, { methodInfo, params: __self.QueryParams, data });
    });
  }

  judgeIsCallApi(PathInfo) {
    if (PathInfo.ext === '') {
      return true;
    }
    let charset = "binary";
    switch (PathInfo.ext) {
      case ".js":
        this.res.writeHead(200, { "Content-Type": "text/javascript" });
        break;
      case ".css":
        this.res.writeHead(200, { "Content-Type": "text/css" });
        break;
      case ".gif":
        charset = "binary";
        this.res.writeHead(200, { "Content-Type": "image/gif" });
        break;
      case ".jpg":
        charset = "binary";
        this.res.writeHead(200, { "Content-Type": "image/jpeg" });
        break;
      case ".png":
        charset = "binary";
        this.res.writeHead(200, { "Content-Type": "image/png" });
        break;
      default:
        this.res.writeHead(200, { "Content-Type": "application/octet-stream" });
    }

    const { dir, ext, name } = PathInfo;
    const __abs = path.join(dir, name + ext);
    const _pathInfo = [path.join('./server/', __abs), path.join('.', __abs)];
    const __self = this;
    let __fileIsExist = false;
    for (let i = 0; i < _pathInfo.length; i++) {
      const dir = _pathInfo[i];
      __fileIsExist = fs.existsSync(dir);
      if (__fileIsExist) {
        fs.readFile(dir, (err, data) => {
          if (err) {
            __self.res.Send({ code: -1, msg: err.toString() });
          } else {
            __self.res.write(data, charset);
          }
          __self.res.end();
        });
        return false;
      }
    }
    if (!__fileIsExist) {
      __self.res.end();
    }
    return false;
  }

  parseUrlParams() {
    const _url = url.parse(this.req.url);
    this.UrlInfo = _url;
    const { query } = _url;
    this.QueryParams = querystring.parse(query);
  }

  __FindMethod(PathInfo, isSendMsg) {
    const { pathname } = this.UrlInfo;
    const pathList = pathname.split('/');
    pathList.shift();
    if (pathList.length === 1) {
      if (isSendMsg) {
        this.res.Send_404({ status: 404, msg: pathname + '接口没有找到' });
      }
      return null;
    }
    const __last = pathList.pop();
    let __CallApi = this.ApiInfo[pathList[0]];
    let __ApiIsExist = true;
    for (let i = 1; i < pathList.length; i++) {
      __CallApi = __CallApi[pathList[i]];
      if (!__CallApi) {
        __ApiIsExist = false;
        break;
      }
    }
    if (!__ApiIsExist) {
      if (isSendMsg) {
        this.res.Send_404({ status: 404, msg: pathname + '接口没有找到' });
      }
      return null;
    }
    const Controller = __CallApi;
    __CallApi = __CallApi[this.Method + '_' + __last]
    if (!__CallApi) {
      if (isSendMsg) {
        this.res.Send_404({ status: 404, msg: pathname + '接口没有找到' });
      }
      return null;
    }

    return { func: __CallApi, ctrl: Controller };
  }
}
module.exports = routes ;

现在接口的调用基本就OK了。
如果有什么不清楚,可以查看 https://github.com/xiaotuni/angular-map-http2 这里项目里具体写了怎么实现接口调用的

原文地址:https://www.cnblogs.com/xiaotuni/p/7286485.html