https://github.com/weivea/fastify-service
在复杂业务场景下用于做业务逻辑封装的一个抽象层, 保持业务逻辑的独立性
https://github.com/weivea/fastify-service
Last synced: 20 days ago
JSON representation
在复杂业务场景下用于做业务逻辑封装的一个抽象层, 保持业务逻辑的独立性
- Host: GitHub
- URL: https://github.com/weivea/fastify-service
- Owner: weivea
- Created: 2018-03-27T12:48:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-27T12:48:33.000Z (about 8 years ago)
- Last Synced: 2025-03-06T04:29:25.174Z (over 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fastiy-servise
fastiy-servise 为control提供service,
用法类似 egg 的service, **兼容egg service 写法**
就不用再把臃肿的代码写在 control里边了
service下的方法 可以通过`this.ctx.servics.xxx` 或 `this.request.servics.xxx` 相互调用,
`this.request`即为每次请求的 request
支持`async/await`方法
## install
```
npm install fastiy-servise --save
```
## usage
index.js
```javascript
const service = require('fastify-service')
const fastify = require('fastify')()
fastify.register(service, {
serviceRoot: path.join(process.cwd(), 'service/')
})
app.get('/api', async (req, res) => {
req.service.testb.bFun()
req.service.aa.funA()
await req.service.testb.b2Fun()
return {
hello: 'ServerData'
}
})
```
service目录
```
.
./aa.js
./testa
./testa/testd.js
./testa.js
./testb.js
./testc
./testc/testc.js
```
./testb.js
```javascript
class Servic {
bFun() {
// service之间相互调用
this.ctx.service.testc.testc.bFun()
console.log(__filename);
return __filename;
}
async b2Fun() {
return await (() =>
new Promise((resolve, reject) => {
// 自运行返回Promise
setTimeout(() => {
console.log('async', __filename)
resolve(__filename);
}, 500);
}))();
}
}
module.exports = Servic;
```