This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
If the heart yearns for what fear road resistance and long
Continuously updated…
Encapsulation method portal:
- Egg Introduction Basics Egg introduction to project creation
- Egg Project Directory and controller
- Egg Basic Egg unit tests
- Egg Basic HTTP request
- Egg Basic knowledge Egg Service Service
Introduction to the
HTTP: / / HTTP: / / HTTP: / / HTTP: / / HTTP: / / HTTP: / / HTTP: / / HTTP: / / HTTP: / / A Service is an abstraction layer used to encapsulate business logic in complex business scenarios. So why Service? In practice, controllers generally do not produce their own data, nor do they contain complex logic, so complex processes should be abstracted as business logic layer Services.
Benefits:
- keep
Controller
The logic is more concise. - Keep business logic independent and abstract
Service
Can be multipleController
Repeat the call. - Separating logic from presentation makes it easier to write test cases.
newService
-
Create a new service under the app file and a new article. Js file under the service
-
In app/service/article. Js, you can see that the naming conventions of service and writing are very similar to Controller. Methods in a Service are also asynchronous methods, so use the async keyword
'use strict'; const Service = require('egg').Service; class ArticleService extends Service { async getArticle(id) { // You should go to the database to query the corresponding article details // const article = await this.ctx.query(`select * from articles where id = ? `, id) // There is no database connection, so the simulated data is used here const article = { id: id, title: '100 VUE trivia Things Rain Creek Didn't know'.content: 'This is content, this is content, this is content, this is content, this is content.'.author: 'vience' } returnarticle; }}module.exports = ArticleService; Copy the code
-
Write the getArticle() method to getArticle details in app/controller/artile.js, By CTX. Service. Article. GetArticle () call service in getArticle () method. GET from ctx.params using the strict GET mode parameter.
// Get the article by id async getArticle() { const { ctx } = this; const res = await ctx.service.article.getArticle(ctx.params.id); ctx.body = res } Copy the code
-
Configure the routing address of the above method in app/router.js
/ / access path: http://127.0.0.1:7001/getArticle/0002 router.get('/getArticle/:id', controller.article.getArticle); Copy the code
-
Use the following command to run projects, and then type http://localhost:7001/getArticle/0001 in your browser
# use npm npm run dev open http://localhost:7001/ # use yarn yarn dev open http://localhost:7001 Copy the code
-
The effect
Matters needing attention
-
The Service file must be stored in the app/ Service directory. Multi-level directories can be supported. When accessing the file, the directory name can be cascaded.
app/service/biz/user.js => ctx.service.biz.user app/service/sync_user.js => ctx.service.syncUser app/service/HackerNews.js => ctx.service.hackerNews Copy the code
-
A Service file can contain only one class, which is returned via module.exports.
-
A Service needs to be defined as a Class, and its parent must be egg.service.
-
A Service is not a singleton, but a request-level object. The framework delays instantiation when it first accesses ctx.service.xx in each request, so the context of the current request can be obtained in a Service through this.ctx.
Write in the last
Follow me, more content continues to output
🌹 if you like, give it a thumbs up 👍🌹
🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹
🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹