egg.js的基本使用和調(diào)用數(shù)據(jù)庫(kù)的方法示例
首先,整個(gè)項(xiàng)目的創(chuàng)建命令:
npm i egg-init -g //安裝egg egg-init egg-example --type=simple //初始化一個(gè)egg模板例子 后面的type跟的是模板類型這里是簡(jiǎn)單的 cd egg-example//進(jìn)入例子 npm i //安裝依賴
可以去官方教程查看基本配置的講解。
直接說(shuō)使用,簡(jiǎn)單看過(guò)Egg的文檔,官方文檔對(duì)于入門還是很實(shí)用的,再配合一些別人的攻略很容易入門上手就可以使用。
首先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ī)則,所有的請(qǐng)求都會(huì)通過(guò)這個(gè)路由規(guī)則去找對(duì)應(yīng)的Controller,這樣也可以做到統(tǒng)一管控(如同前端初始化所有組件吧)。
接下來(lái)就是Controller控制層:
'use strict';
const Controller = require('egg').Controller;
class CustomController extends Controller {
async custonIndex() { //注意這里要定義成異步方法防止請(qǐng)求被阻塞
//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ā)送請(qǐng)求會(huì)調(diào)用Controller中的方法,Controller中主要工作是接受用戶的參數(shù)并進(jìn)行處理,然后將處理好的參數(shù)發(fā)送給Service層,然后把Service的結(jié)果返回給用戶。
其中對(duì)參數(shù)的處理包括但不僅限于參數(shù)校驗(yàn)和參數(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í)行對(duì)數(shù)據(jù)庫(kù)或者其他操作,最終將結(jié)果返回,一個(gè)請(qǐng)求的簡(jiǎn)單流程就算是完成了
配置MySQL在egg-project\config\config.default.js里面,直接放上我的配置,具體起她的數(shù)據(jù)庫(kù)配置方法可以自查。
'use strict';
module.exports = appInfo => {
const config = exports = {
mysql:{
// 單數(shù)據(jù)庫(kù)信息配置
client: {
// host
host: '44.44.44.44',
// 端口號(hào)
port: '3306',
// 用戶名
user: 'mysq',
// 密碼
password: '359359',
// 數(shù)據(jù)庫(kù)名
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ù)庫(kù)之間的基本操作了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript for循環(huán)性能測(cè)試示例
這篇文章主要介紹了javascript for循環(huán)性能測(cè)試,結(jié)合實(shí)例形式分析了javascript使用for循環(huán)遍歷數(shù)組的三種常用方法及對(duì)應(yīng)的時(shí)間消耗,總結(jié)javascript使用for循環(huán)遍歷數(shù)組的相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
js表數(shù)據(jù)排序 sort table data
對(duì)于表格的排序,是很不錯(cuò)的一個(gè)功能,方便用戶快速的分析一些數(shù)據(jù)。2009-02-02
借用Google的Javascript API Loader來(lái)加速你的網(wǎng)站
加速頁(yè)面加載速度有一個(gè)方法就是把CSS和JS文件放到另外一個(gè)單獨(dú)的服務(wù)器上,這樣在訪問(wèn)量比較大的情況下可以分擔(dān)主服務(wù)器的壓力2009-01-01
JS實(shí)現(xiàn)“隱藏與顯示”功能(多種方法)
這篇文章主要介紹了JS實(shí)現(xiàn)“隱藏與顯示”功能的多種方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
javascript中日期轉(zhuǎn)換成時(shí)間戳的小例子
javascript中日期轉(zhuǎn)換成時(shí)間戳的小例子,需要的朋友可以參考一下2013-03-03

