Coder Social home page Coder Social logo

egg-api's Introduction

Egg-API

使用Egg.js创建后台服务,提供API接口

技术栈: Egg.js、MongoDB、mongoose、MySQL

所有API接口在postman上通过测试,说明如下:

  • API请求返回的内容包括code(响应结果标识码),msg(响应结果信息说明),data(响应数据)
  • 接口数据采用json格式
  • 安全性,会做好一些限制,比如鉴权或认证
  • 接口的性能
  • RESTful API

接口模块:

  • 用户模块(注册登录)(user)
  • 项目(projects)
  • 文件上传删除(files)
  • MySQL demo

另:

使用前安装配置MongoDB和MySQL数据库,如果没有记得先注释掉 /config/config.default.js 里配置数据库的语句块(注意: 我们的数据是基于数据库的)

demo访问格式如下: http://127.0.0.1:7001/public/examples/index.html

项目正在持续更新中,喜欢的请点心,关注,star ,fork,这些是我长期坚持下去的动力 (。_ 。) ✎_

QuickStart

Development

$ npm i
$ npm run dev
$ open http://localhost:7001/

Deploy

$ npm start
$ npm stop

npm scripts

  • Use npm run lint to check code style.
  • Use npm test to run unit test.
  • Use npm run autod to auto detect dependencies upgrade, see autod for more detail.

egg-api's People

Contributors

levinhax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

egg-api's Issues

deleteuser验证失败后中间件errorHandler捕获不到插件egg-validate报的422错误

测试方法:api/v1/deleteuser
参数{"_id":"5c0e0db0f920d60a6d8dae04","userRole":"1","deleteUserId":"u1"}

返回状态码:404
Response Body:{"message":"Not Found"}

------分割线--------
请求数据中,用户角色是1。不满足 controller.user.deleteUser中userRole必须是3的rule.
ctx.validate失败,抛出422错误。
但是外面的errorHandler没有捕获到这个错误。

userMsg: {"_id":"5c0e0db0f920d60a6d8dae04","userRole":"1","deleteUserId":"u1"} { UnprocessableEntityError: Validation Failed at Object.throw (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa/lib/context.js:97:11) at Object.validate (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/egg-validate/app/extend/context.js:14:17) at UserController.deleteUser (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/app/controller/user.js:106:15) at Object.callFn (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/egg-core/lib/utils/index.js:36:21) at Object.classControllerMiddleware (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/egg-core/lib/loader/mixin/controller.js:87:20) at Object.callFn (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/egg-core/lib/utils/index.js:36:21) at wrappedController (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/egg-core/lib/utils/router.js:322:18) at dispatch (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/node_modules/koa-compose/index.js:44:32) at next (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/node_modules/koa-compose/index.js:45:18) at /Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/lib/router.js:346:16 at dispatch (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/node_modules/koa-compose/index.js:44:32) at /Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/node_modules/koa-compose/index.js:36:12 at dispatch (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-router/lib/router.js:351:31) at dispatch (/Users/leo/VSCodeProjects/极刊登/ji-listing-server/node_modules/koa-compose/index.js:42:32) at /Users/leo/VSCodeProjects/极刊登/ji-listing-server/app/middleware/jwt.js:30:17 at process._tickCallback (internal/process/next_tick.js:68:7) message: 'Validation Failed', code: 'invalid_param', errors: [ { message: 'should be one of 3', code: 'invalid', field: 'userRole' } ] }

npm run dev

egg deprecated [egg-security] app.config.security.csrf.ignoreJSON is not safe now, please disable it. node_modules_egg-core@4.11.0@egg-core\lib\lifecycle.js:93:9
2018-11-16 22:27:21,493 ERROR 6904 [egg-mongoose] mongodb://127.0.0.1/apiExample disconnected
2018-11-16 22:27:21,497 ERROR 6904 nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: "MongoNetworkError"
errorLabels: ["TransientTransactionError"]
pid: 6904
hostname: DESKTOP-106KQGJ

2018-11-16 22:27:21,503 ERROR 6904 [-/127.0.0.1/-/1ms GET /] nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: "MongoNetworkError"
errorLabels: ["TransientTransactionError"]
pid: 6904
hostname: DESKTOP-106KQGJ

2018-11-16 22:27:21,508 ERROR 6904 nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket. (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_mongodb-core@3.1.7@mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: "MongoNetworkError"
errorLabels: ["TransientTransactionError"]
pid: 6904
hostname: DESKTOP-106KQGJ

2018-11-16 22:27:21,511 ERROR 6904 [app_worker] start error, exiting with code:1
[2018-11-16 22:27:21.524] [cfork:master:4972] worker:6904 disconnect (exitedAfterDisconnect: false, state: disconnected, isDead: false, worker.disableRefork: true)
2018-11-16 22:27:21,528 WARN 7056 [ClusterClient:Connection] socket is closed by other side while there were still unhandled data in the socket buffer
[2018-11-16 22:27:21.530] [cfork:master:4972] don't fork, because worker:6904 will be kill soon
2018-11-16 22:27:21,533 INFO 4972 [master] app_worker#1:6904 disconnect, suicide: false, state: disconnected, current workers: ["1"]
[2018-11-16 22:27:21.538] [cfork:master:4972] worker:6904 exit (code: 1, exitedAfterDisconnect: false, state: dead, isDead: true, isExpected: false, worker.disableRefork: true)
2018-11-16 22:27:21,541 ERROR 4972 nodejs.AppWorkerDiedError: [master] app_worker#1:6904 died (code: 1, signal: null, suicide: false, state: dead), current workers: []
at Master.onAppExit (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_egg-cluster@1.22.1@egg-cluster\lib\master.js:398:21)
at Master.emit (events.js:182:13)
at Messenger.sendToMaster (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_egg-cluster@1.22.1@egg-cluster\lib\utils\messenger.js:133:17)
at Messenger.send (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_egg-cluster@1.22.1@egg-cluster\lib\utils\messenger.js:98:12)
at EventEmitter.cluster.on (C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_egg-cluster@1.22.1@egg-cluster\lib\master.js:267:22)
at EventEmitter.emit (events.js:187:15)
at ChildProcess.worker.process.once (internal/cluster/master.js:193:13)
at Object.onceWrapper (events.js:273:13)
at ChildProcess.emit (events.js:182:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
name: "AppWorkerDiedError"
pid: 4972
hostname: DESKTOP-106KQGJ

2018-11-16 22:27:21,543 ERROR 4972 [master] app_worker#1:6904 start fail, exiting with code:1
2018-11-16 22:27:21,545 ERROR 4972 [master] exit with code:1
⚠️ Error: C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules_egg-bin@4.9.0@egg-bin\lib\start-cluster {"baseDir":"C:\Users\root\Downloads\Egg-API-master\Egg-API-master","workers":1,"framework":"C:\Users\root\Downloads\Egg-API-master\Egg-API-master\node_modules\egg"} exit with code 1
⚠️ Command Error, enable DEBUG=common-bin for detail
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: egg-bin dev
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\root\AppData\Roaming\npm-cache_logs\2018-11-16T14_27_21_597Z-debug.log

sql file?

Hello, where is the SQL file?

Thank you for a copy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.