首先,整個項目的創(chuàng)建命令:
創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元湯陰做網(wǎng)站,已為上家服務(wù),為湯陰各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
npm i egg-init -g //安裝egg egg-init egg-example --type=simple //初始化一個egg模板例子 后面的type跟的是模板類型這里是簡單的 cd egg-example//進入例子 npm i //安裝依賴
可以去官方教程查看基本配置的講解。
直接說使用,簡單看過Egg的文檔,官方文檔對于入門還是很實用的,再配合一些別人的攻略很容易入門上手就可以使用。
首先router.js:
'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; router.get('/', controller.home.index); router.get('/custom',controller.customController.custonIndex); //自定義controller的路由 };
router.js中定義了我們的路由規(guī)則,所有的請求都會通過這個路由規(guī)則去找對應(yīng)的Controller,這樣也可以做到統(tǒng)一管控(如同前端初始化所有組件吧)。
接下來就是Controller控制層:
'use strict'; const Controller = require('egg').Controller; class CustomController extends Controller { async custonIndex() { //注意這里要定義成異步方法防止請求被阻塞 //let {id} = this.ctx.params; // 獲取路由參數(shù) //let {name} = this.ctx.query; // 獲取用戶入?yún)? let options = {id:'5', name:2} let info = await this.ctx.service.customService.getInfo(options);//調(diào)用Service層傳參 處理,返回結(jié)果賦值 this.ctx.body = { code: 200, data: info };//返回體 this.ctx.status = 200; } } module.exports = CustomController;
發(fā)送請求會調(diào)用Controller中的方法,Controller中主要工作是接受用戶的參數(shù)并進行處理,然后將處理好的參數(shù)發(fā)送給Service層,然后把Service的結(jié)果返回給用戶。
其中對參數(shù)的處理包括但不僅限于參數(shù)校驗和參數(shù)拼裝,當(dāng)然也可以直接返回不走Service,都在Controller層做處理,但是不建議這樣做。
服務(wù)層(Service):
const Service = require('egg').Service; class CustimService extends Service { async getInfo(options) { const results = await this.app.MySQL.select('test',{id:5}); return results[0].name; } } module.exports = CustimService;
Service層拿到Controller層的數(shù)據(jù)之后,根據(jù)條件執(zhí)行對數(shù)據(jù)庫或者其他操作,最終將結(jié)果返回,一個請求的簡單流程就算是完成了
配置MySQL在egg-project\config\config.default.js里面,直接放上我的配置,具體起她的數(shù)據(jù)庫配置方法可以自查。
'use strict'; module.exports = appInfo => { const config = exports = { mysql:{ // 單數(shù)據(jù)庫信息配置 client: { // host host: '44.44.44.44', // 端口號 port: '3306', // 用戶名 user: 'mysq', // 密碼 password: '359359', // 數(shù)據(jù)庫名 database: 'mysql_db', }, // 是否加載到 app 上,默認(rèn)開啟 app: true, // 是否加載到 agent 上,默認(rèn)關(guān)閉 agent: false, } }; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_17792_5967'; // add your config here config.middleware = []; return config; };
這樣,你就打通了egg和數(shù)據(jù)庫之間的基本操作了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章題目:egg.js的基本使用和調(diào)用數(shù)據(jù)庫的方法示例
本文網(wǎng)址:http://bm7419.com/article26/igogcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、Google、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)