Node.js Express + MongoDB 实现增删查改接口(CRUD)以及登录/注册获取Token API

Express + MongonDB 实现RESTFUL风格基本增删查改已经登陆注册获取Token接口

.env文件里MONGODB_URL为mongodb数据库地址,testmongo为数据库名, PORT为服务端端口默认为3000

  • 实现对数据的基本增删改查
  • 实现用户的登陆注册获取token

仓库地址

https://github.com/yuanchao614/Mongodb-Express-CRUD

setUp

git clone 

cd Mongodb-Express-CRUD

npm install

npm run dev

Example API

  • query all data

不带分页参数默认查询所有数据

header: token
methods: GET
URL: http://localhost:3000/api/v1/user/?pageSize=10&pageIndex=0
Return results: 
{
  data: [],
  total: number,
  msg: ''
}

查询数据

  • query by id
header: token
methods: GET
URL: http://localhost:3000/api/v1/user/<id>
Return results: []

根据ID查询数据

  • create one
header: token
methods: POST
URL: http://localhost:3000/api/v1/user/
bodyParam: 
{
    userName: 'admin',
    passWord: 'admin'
    email: 'admin@qq.com'
}
Return results:
{
  data: {},
  msg: ''
}

新增数据

  • update one
header: token
methods: POST
URL: http://localhost:3000/api/v1/user/<id>
params: id
bodyParam: 
{
    userName: 'admin',
    passWord: 'admin'
    email: 'admin@qq.com'
}
Return results: 
{
  data: '',
  msg: ''
}

更新数据

  • delete one
header: token
methods: DELETE
URL: http://localhost:3000/api/v1/user/<id>
params: id
Return results: {}

删除数据

  • signup
methods: POST
URL: http://localhost:3000/api/v1/auth/signup
body: {
userName,
email,
passWord
}
Return results: 
{
  token,
  data: {},
  msg: ''
}

用户注册

  • login
methods: POST
URL: http://localhost:3000/api/v1/auth/login
body: {
email,
passWord
}
Return results: {
token,
data: {},
msg: ''
}

用户登录

  • query by userData count by createDate
header: token
methods: GET
URL: http://localhost:3000/api/v1/user/query/querybyDate
Return results: {
  data: [],
  msg: ''
}

查询用户统计信息

插件

  • mongodb插件mongoose

https://www.npmjs.com/package/mongoose

  • bcryptjs加密插件

bcryptjs

  • jsonwebtoken生成token插件

jsonwebtoken

  • nodemon自动更新插件

nodemon

  • morganapi日志插件

morgan

posted @ 2020-11-16 18:05  Michelyuan  阅读(518)  评论(0编辑  收藏  举报