安装软件

我们需要安装 MongoDB 和一个可视化工具 Robo 3T

MongoDB 的安装教程可以参考 Windows 平台安装 MongoDB

MongoDB

MongoDB 服务启动及停止:

1
2
net start mongodb
net stop mongodb

启动服务后可以去预览 http://127.0.0.1:27017/,如果网页上显示了 It looks like you are trying to access MongoDB over HTTP on the native driver port. ,就表示启动成功。

注意:需要将 MongoDB 添加到系统的环境变量中。


Robo 3T

进入软件后点击 Createname 随意设置,Address 设置为 localhost,端口设置为 27017,然后点击 save 即可。

默认情况下会看见 System 和 config 两个文件夹。


koa-generator

全局安装 koa-generator

1
npm i -g koa-generator

创建项目:

1
2
3
koa2 项目名
// OR
koa2 -e 项目名

默认是 jade,添加 -e 则表示使用 ejs 引擎。

下面我们就来创建一个新项目:

1
2
3
4
koa2 -e koa2-demo
cd koa2-demo
npm i
npm run dev

然后打开 localhost:3000,即可预览。


mongoose

koa2-demo 目录下安装 mongoose:

1
npm i mongoose

然后新建 dbs文件夹及相应的配置文件:

1
2
3
mkdir dbs
cd dbs
touch config.js

然后编辑 config.js 文件,配置数据库选项:

1
2
3
module.exports = {
dbs: 'mongodb://127.0.0.1:27017/dbs'
}

再在 dbs 下新建一个目录及文件:

1
2
3
mkdir models
cd models
touch person.js

编辑 person.js 文件:

1
2
3
4
5
6
7
8
9
10
// 引入mongoose
const mongoose = require('mongoose');

// 建一个Schema
let personSchema = new mongoose.Schema({
name: String,
age: Number
})

module.exports = mongoose.model('Person', personSchema)

app.js 中引入:

1
2
3
4
5
6
7
const mongoose = require('mongoose')
const dbConfig = require('./dbs/config')

// 连接数据库
mongoose.connect(dbConfig.dbs, {
useNewUrlParser: true
})

最后使用 npm run dev 运行即可。


对数据库进行增删改查操作

我们在 routes/users.js 文件下进行编辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 引入person
const Person = require('../dbs/models/person')

// 新增接口
router.post('/addPerson', async function (ctx) {
const person = new Person({
name: ctx.request.body.name,
age: ctx.request.body.age
})
let code
try {
await person.save()
code = 0
} catch (e) {
code = -1
}
ctx.body = {
code: code
}
})

然后我们可以使用此接口来检验是否成功。

注意:这是 post,不能在浏览器上直接输入请求,可以使用 Postman

Postman 中输入下面地址:

1
http://localhost:3000/users/addPerson

然后在 body 中设置 key、value 即可。

请求成功后,我们可以去 最开始下载的 Robo 3T 中查看。(需要先启动mongodb服务)

当进入 Robo 3T 后,刷新一下,就会发现有个 dbs,然后点击 Collections,然后双击 people,就可以看到刚才请求成功后的数据。

下面我们还可以添加 删、改、查 的接口,下面这是 users.js 文件的完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const router = require('koa-router')()

const Person = require('../dbs/models/person')

router.prefix('/users')

router.get('/', function (ctx, next) {
ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
ctx.body = 'this is a users/bar response'
})

// 增
router.post('/addPerson', async function (ctx) {
const person = new Person({
name: ctx.request.body.name,
age: ctx.request.body.age
})
let code
try {
await person.save()
code = 0
} catch (e) {
code = -1
}
ctx.body = {
code: code
}
})

// 查
router.post('/getPerson', async function (ctx) {
const result = await Person.findOne({
name: ctx.request.body.name
})
const results = await Person.find({
name: ctx.request.body.name
})
ctx.body = {
code: 0,
result,
results
}
})

// 改
router.post('/updatePerson', async function (ctx) {
const result = await Person.where({
name: ctx.request.body.name
}).update({
age: ctx.request.body.age
})
ctx.body = {
code: 0
}
})

// 删
router.post('/removePerson', async function (ctx) {
const result = await Person.where({
name: ctx.request.body.name
}).remove()

ctx.body = {
code: 0
}
})

module.exports = router

Redis

简介

Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库。

Redis 与其他 key - value 缓存产品有以下三个特点:

  1. Redis 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
  2. Redis 不仅仅支持简单的 key-value 类型的数据,同时还提供 list,set,zset,hash 等数据结构的存储。
  3. Redis 支持数据的备份,即 master-slave 模式的数据备份。

安装

Redis 安装

1
redis-server.exe redis.windows.conf
1
2
3
redis-server --service-start    // 开启服务
redis-server --service-stop // 停止服务
redis-server --service-uninstall // 卸载服务

安装中间件

1
npm i koa-generic-session koa-redis

使用

app.js 文件中引入:

1
2
3
4
5
6
7
8
9
const session = require('koa-generic-session')
const Redis = require('koa-redis')

app.keys = ['keys', 'keyskeys']
app.use(session({
key: 'mt',
prefix: 'mtpr',
store: new Redis()
}))

然后在 routes/users.js添加如下代码:

1
2
3
4
5
6
7
8
9
const Redis = require('koa-redis')
const Store = new Redis().client

router.get('/fix', async function (ctx) {
const st = await Store.hset('fix', 'name', Math.random())
ctx.body = {
code: 0
}
})

预览 http://localhost:3000/users/fix

然后新开一个命令窗口:

1
2
redis-cli
keys *

我们就会发现输出了 fix


相关资料

Koa - 中文文档

mongoose4.5中文教程

Redis 教程