接口中作建立實例redis
# npm in mongoose # mkdir dbs # mkdir dbs/models # touch dbs/models/person.js # touch dbs/config.js
config.js=》數據庫地址
mongodb
module.exports = { dbs: 'mongodb://127.0.0.1:27017/dbs' }
person.js=》先聲明Schema,在數據庫中聲明一個表,建立模型,在模型中建實例
數據庫
const mongoose = require('mongoose') const personSchema =new mongoose.Schema({ name:String, age:Number }) module.exports=mongoose.model('Perosn',personSchema)
app.js=>引入mongoose和數據庫地址,進行鏈接
npm
const mongoose = require('mongoose') const dbConfig = require('./dbs/config') mongoose.connect(dbConfig.dbs, { useNewUrlParser: true })
routes/users.js
cookie
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 { code = -1; } ctx.body = { code: code } }) // 刪除 router.post('/removePerson', async function (ctx) { let resa = await Person.where({ name: ctx.request.body.name }).remove() let code; try { code = 0; } catch (error) { code = -1; } ctx.body = { code: code } }) // 修改更新 router.post('/updatPerson', async function (ctx) { await Person.where({ name: ctx.request.body.name }).update({ age: ctx.request.body.age }) ctx.body = { code: 0 } }) // 查詢 router.post('/findPerson', async function (ctx) { let resonlv1 = await Person.findOne({ name: ctx.request.body.name }) let resonlv2 = await Person.find({ name: ctx.request.body.name }) ctx.body = { code: 0, resonlv1, resonlv2 } }) module.exports = router
終端啓動請求接口
session
# curl=》發起請求;-d =》post請求 # 新增 curl -d "name=youzi&age=18" http://localhost:3000/users/addPerson # 刪除 curl -d "name=youzi" http://localhost:3000/users/removePerson # 更新修改 curl -d "name=youzi" http://localhost:3000/users/updatPerson # 查新 curl -d "name=youzi" http://localhost:3000/users/findPerson
提及咱們平時工做中常開發的登錄功能,服務端的程序是如何識別客戶端的狀態呢?HTTP是無狀態的,用戶訪問了咱們服務端的程序,怎麼保證下次訪問的時候仍是這個用戶呢?服務端的session又是如何保持在客戶端呢?
app
app.js引入2箇中間件,進行開發
dom
const session = require('koa-generic-session') const Redis = require('koa-redis') app.keys = ['keys', 'keyskyes']; //對session進行加密,這裏是值本身定哦 app.use(session({ store:new Redis() //不寫配置項內容存進內存,這裏咱們存到redis中 }))
koa-pv.js中間件
koa
# 這裏記錄pv數加加。將session和當前用戶訪問進行關聯 # 將session的值存在cookie中,區分不一樣的用戶身份 function pv(ctx) { ctx.session.count++ global.console.log('pv' + ctx.path) } module.exports = function () { return async function (ctx, next) { pv(ctx) await next() } }
此時刷新頁面查看。cookie中已經有咱們剛存進去的值了koa開頭的就是咱們儲存進去的內容,這裏koa開頭的key值咱們是能夠去修改的。經過key和前綴prefix設置便可,用法以下
curl
app.use(session({ key: 'mt', prefix: 'mtpr', store: new Redis() }))
接口中寫入
const Redis = require('koa-redis') const Store = new Redis().client; router.get('/fix', async function (ctx) { const st = await Store.hset('fix', 'nanme', Math.random()) ctx.body = { code: 0 } }) # 直接請求便可。由於是get,哈哈哈哈 # url http://localhost:3000/users/fix # 去redis中查詢就能看到咱們剛纔建立的值啦