MongooDB

  1. 安装,下载地址:https://www.mongodb.com/try/download/community
  2. 添加安装路径下的bin目录至,全局变量环境中的path
  3. 启动,在管理员模式下运行:net start MongoDB
  4. 停止,在管理员模式下运行:net stop MongoDB

    Egg

    初始化项目
    1. npm init egg demo1
    image.png

    关联数据库

    根据readme中步骤安装:https://github.com/eggjs/egg-mongoose

  1. npm i egg-mongoose

参数校验,中文提示配置

根据readme中步骤安装:https://github.com/eggjs/egg-validate

  1. npm i egg-validate i18n

配置

  1. const I18n = require('i18n');
  2. I18n.configure({
  3. locales: [ 'zh-CN' ],
  4. defaultLocale: 'zh-CN',
  5. directory: __dirname + '/locale',
  6. });
  7. config.validate = {
  8. // convert: false,
  9. // validateRoot: false,
  10. translate() {
  11. const args = Array.prototype.slice.call(arguments);
  12. return I18n.__.apply(I18n, args);
  13. },
  14. };

统一错误处理

具体参照:https://www.eggjs.org/zh-CN/tutorials/restful#%E7%BB%9F%E4%B8%80%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86

生成token

  1. npm install jsonwebtoken

路由

设置基础路径

  1. module.exports = app => {
  2. ···
  3. router.prefix('/api/v1');
  4. ···
  5. };

Apollo

https://www.npmjs.com/package/node-apollo-client

  1. // Apollo
  2. const Apollo = async () => {
  3. const app = require('node-apollo-client');
  4. const apollo = new app({
  5. configServerUrl: 'http://apollo.58huihuahua.com:18081',
  6. appId: 'word-admin-api',
  7. cluster: 'default',
  8. namespaces: [ 'application' ],
  9. fetchCacheInterval: 5 * 60e3,
  10. });
  11. const result = await apollo.fetchConfig({ key: 'mongoUrl' });
  12. console.log('返回内容:', result);
  13. };
  14. Apollo();