moon-easy-api:自动化生成基于MongoDB的接口

前言

我是一个比较喜欢前端的爱好者,我其实很多时候都有做一些个人小项目demo的计划。

但是总是遇到一个问题。

我不想写接口!!

虽然也许只是很简单的一些增删改查也让我觉得很麻烦。

我学过java,但是让我为了做一个小demo用java整一套ssm的后端服务想想就麻烦。

我又想到我还用过MongoDB,我的本地机和租借的云服务器都部署好了MongoDB,结合nodejs,不考虑企业级的规范,用express只要写app.js一个文件直接执行起来,就能有可以用的接口了。

只是写起来还是很麻烦,我也不需要特别复杂的调用,只要能支持我的小项目使用的增删改查就够,都是一些重复的东西,于是我萌生一个想法,重复的内容封装起来,用文件配置的形式,只要添加一个文件,运行起来接口就给我部署好了,于是我开始尝试…

效果

express的基础服务器框架,你只需要创建一个app的实例,连接自己的mongodb数据库,然后导入我上传的npm包,将app实例和数据库mongoose传入方法即可。
在这里插入图片描述

在你的服务端根目录创建一个apiModel文件夹(必须是该名称),然后可以添加你要的数据对象,就是用于生成数据库表信息的一个对象,按类似形式导出。
在这里插入图片描述
运行app.js。

在这里插入图片描述

试试

新建一个项目,test
生成package.json

npm init -y

运行结果

C:\Users\chenshaojun\Desktop\test>npm init -y
Wrote to C:\Users\chenshaojun\Desktop\test\package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

我们可能需要添加对es6模块的支持。
在这里插入图片描述

然后在根目录npm下载我的npm包,名字叫做moon-easy-api。

npm i moon-easy-api -S

运行结果

C:\Users\chenshaojun\Desktop\test>npm i moon-easy-api -S
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.

added 403 packages, and audited 404 packages in 33s

16 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

完成后编写我们的app.js。
在这里插入图片描述

import express from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import easyApi from 'moon-easy-api';
const app = express();

app.use(bodyParser.json());

//连接你的mongodb数据库注意数据库名我的是mydata
//你的自己创建好
mongoose.connect("mongodb://81.68.155.48:27017/mydata");

easyApi(app, mongoose);

app.listen(3000, function () {
  console.log("服务器开启中....端口号是:" + 3000);
});

再编写自己想要的model。
在这里插入图片描述

const model = {
  id: String,
  name: String,
  old: Number,
  sex: String,
};

export { model };

然后就可以运行app.js。
在这里插入图片描述
显示数据库连接成功就是完成对接口的创建了。

测试

成功后会再对应的端口生成可以增删改查的接口,根据我这里的model名字是other,other便会生成addother、deleteother、updateother、getother四个接口。

可以编写一个test.js进行测试
在这里插入图片描述

代码,注意args[0]是你要执行的接口名,在执行命令时传递

import fetch from "node-fetch";
const args = process.argv.slice(2)
const test = async () => {
  const response = await fetch("http://localhost:3000/" + args[0], {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json;charset=UTF-8",
    },
    body: JSON.stringify({
      id: "1",
      name: "月",
      old: 18,
      sex: "男",
    }),
  });
  const res = await response.json();
  console.log(res);
};

test();

需要的fetch包下载一下

npm i node-fetch -D

添加test命令
在这里插入图片描述
试试增查删

npm run test addother
npm run test getother
npm run test deleteother
C:\Users\chenshaojun\Desktop\test>npm run test addother

> test@1.0.0 test
> node test.js "addother"

(node:11188) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{
  data: {
    _id: '61371d7081c5ef1c1aa87c1a',
    id: '1',
    name: '月',
    old: 18,
    sex: '男',
    __v: 0
  },
  msg: '添加成功'
}

C:\Users\chenshaojun\Desktop\test>npm run test getother

> test@1.0.0 test
> node test.js "getother"

(node:1320) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{
  data: {
    _id: '61371d7081c5ef1c1aa87c1a',
    id: '1',
    name: '月',
    old: 18,
    sex: '男',
    __v: 0
  },
  code: 1,
  msg: '查询成功'
}

C:\Users\chenshaojun\Desktop\test>npm run test deleteother

> test@1.0.0 test
> node test.js "deleteother"

(node:8788) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{ code: 2, msg: '删除成功' }

缺陷

1.目前所有对象必须传递id,因为内容是根据id进行查询的
2.目前没有考虑复杂的情况,关联表之类的都没有,只用于不同对象的增删改查。

补充

我感觉自己这个想法非常有用啊。

不只是用来生成简易接口做demo,工作的时候等不到后台数据我也可以自己创建简易接口自测。

不过目前还只是做了个开头,我想对自己这个想法进行更新迭代,比如可拓展的自定义接口,还是解决一下我这个必须传id的问题。

有没有有同样想法的前端开发者,可以私聊我交流交流,或者有没有厉害的前辈们想要指点一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在下月亮有何贵干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值