九、从零模拟新浪微博-关注和取消关注
代码仓库:https://github.com/changeclass/koa2-weibo 数据模型 模型 123456789101112131415161718192021222324/** * @description:用户关注关系 * @author: 小康 * @url: https://xiaokang.me * @Date: 2020-12-19 15:31:54 * @LastEditTime: 2020-12-19 15:31:55 * @LastEditors: 小康 */const seq = require('../seq')const { INTEGER } = require('../type')const UserRelation = seq.define('userRelation', { userId: { type: INTEGER, allowNull: false, comment: '用户ID' & ...
八、从零模拟新浪微博-广场页面
代码仓库:https://github.com/changeclass/koa2-weibo 广场页与个人页面相似度很高,唯一不同的就是广场页的数据需要进行缓存。 控制器层 1234567891011121314151617181920212223242526272829303132333435/** * @description: 微博广场 * @author: 小康 * @url: https://xiaokang.me * @Date: 2020-12-19 14:38:57 * @LastEditTime: 2020-12-19 14:38:58 * @LastEditors: 小康 */const { getSquareCacheList } = require('../cache/blog')const { PAGE_SIZE } = require('../config/constant')const { SuccessModel } = require('. ...
七、从零模拟新浪微博-个人主页
代码仓库:https://github.com/changeclass/koa2-weibo 用户数据 视图路由层 1234router.get('/profile', loginRedirect, async (ctx, next) => { const { userName } = ctx.session.userInfo ctx.redirect(`/profile/${userName}`)}) 当访问路由没有用户名时,默认跳转到自己用户名下。 123456789101112131415161718192021222324252627router.get('/profile/:userName', loginRedirect, async (ctx, next) => { const { userName: curUserName } = ctx.params // 获取微博第一页数据 // controller c ...
六、从零模拟新浪微博-创建微博
代码仓库:https://github.com/changeclass/koa2-weibo 数据模型 12345678910111213141516171819202122232425262728293031/** * @description: 微博数据模型 * @author: 小康 * @url: https://xiaokang.me * @Date: 2020-12-18 16:05:16 * @LastEditTime: 2020-12-18 16:05:17 * @LastEditors: 小康 */const seq = require('../seq')const { STRING, INTEGER, TEXT } = require('../type')const Blog = seq.define('blog', { userId: { type: INTEGER, allowNull: false, comment: '用户ID& ...
五、从零模拟新浪微博-用户设置
代码仓库:https://github.com/changeclass/koa2-weibo 文件上传 安装插件(https://www.npmjs.com/package/formidable-upload-koa) 1yarn add formidable-upload-koa fs-extra api路由层实现 12345678910111213141516171819202122232425/** * @description:utils api 路由 * @author: 小康 * @url: https://xiaokang.me * @Date: 2020-12-18 14:08:00 * @LastEditTime: 2020-12-18 14:08:03 * @LastEditors: 小康 */const router = require('koa-router')()const koaFrom = require('formidable-upload-koa')const { saveFile } ...
四、从零模拟新浪微博-用户管理
代码仓库:https://github.com/changeclass/koa2-weibo 数据同步 user表数据模型 1234567891011121314151617181920212223242526272829303132333435363738394041/** * @description 用户数据模型 * @author 小康 */const seq = require('../seq')const { STRING, DECIMAL } = require('../type')const User = seq.define('user', { userName: { type: STRING, allowNull: false, unique: true, comment: '唯一' }, password: { type: STRING, ...
三、从零模拟新浪微博-jwt示例
koa2中使用jwt生成TOKEN 安装插件 1yarn add koa-jwt jsonwebtoken 注册中间件 123456789const jwtKoa = require('koa-jwt')// jwtapp.use( jwtKoa({ secret: SECRET }).unless({ path: [/^\/users\/login/] // 定义那些目录忽略jwt验证 })) SECRET是一个加密的密钥,字符串类型。 在登录成功后返回token 123456const jwt = require('jsonwebtoken')const { SECRET } = require('../conf/constants')let tokenif (userInfo) { token = jwt.sign(userInfo, SECRET, { expiresIn: '1h ...
二、从零模拟新浪微博-基本环境搭建
介绍 将用户的session存放在redis中。 安装依赖 1yarn add koa-redis koa-generic-session 配置session 配置session需要在注册路由前进行配置。 1234567891011121314151617181920212223// ...// session配置(加密密匙)app.keys = ['XiaoKang666']app.use( session({ // cookie的name 默认是 koa.sid key: 'weibo.sid', // redis key 的前缀 默认是 koa.sess prefix: 'weibo:sess:', cookie: { path: '/', httpOnly: true, maxAge: 24 * 60 * 60 * 1000 ...
一、从零模拟新浪微博-数据库操作
建表 users column dataType pk主键 nn不为空 AI自动增加 Default id int Y Y Y username varchar(20) Y password varchar(20) Y nickname varchar(10) Y blogs column dataType pk主键 nn不为空 AI自动增加 Default id int Y Y Y title varchar(50) Y content text Y userid int Y sequlize 安装插件 1yarn add mysql2 sequelize -d 创建链接 1234567891011121314151617181920const Sequelise = require('sequelize')const conf = { host: 'localhost', dialect: 'mysql ...












