四、从零模拟新浪微博-用户管理
代码仓库:https://github.com/changeclass/koa2-weibo 数据同步 user表数据模型 /** * @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, allowNull: false, comment: '密码' }, nickName: { type: ST ...
三、从零模拟新浪微博-jwt示例
koa2中使用jwt生成TOKEN 安装插件 yarn add koa-jwt jsonwebtoken 注册中间件 const jwtKoa = require('koa-jwt') // jwt app.use( jwtKoa({ secret: SECRET }).unless({ path: [/^\/users\/login/] // 定义那些目录忽略jwt验证 }) ) SECRET是一个加密的密钥,字符串类型。 在登录成功后返回token const jwt = require('jsonwebtoken') const { SECRET } = require('../conf/constants') let token if (userInfo) { token = jwt.sign(userInfo, SECRET, { expiresIn: '1h' }) } Koa2中获取token中的用户信息 在请 ...
二、从零模拟新浪微博-基本环境搭建
介绍 将用户的session存放在redis中。 安装依赖 yarn add koa-redis koa-generic-session 配置session 配置session需要在注册路由前进行配置。 // ... // 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 }, store: redisStore({ all: `$ ...
一、从零模拟新浪微博-数据库操作
建表 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 安装插件 yarn add mysql2 sequelize -d 创建链接 const Sequelise = require('sequelize') const conf = { host: 'localhost', dialect: 'mysql' } // 数据库名、账户名、密码 const seq = new Sequelise('koa2 ...
8、TypeScript的装饰器
装饰器概念 装饰器-Decorators 在 TypeScript 中是一种可以在不修改类代码的基础上通过添加标注的方式来对类型进行扩展的一种方式 减少代码量 提高代码扩展性、可读性和维护性 在 TypeScript 中,装饰器只能在类中使用 装饰器的使用 使用装饰器需要配置文件中启用experimentalDecorators: true // 定义方法装饰器 function log(target: Function, name: string, descriptor: PropertyDescriptor) { /** * target : 被装饰的方法所属的类 * name : 当前被装饰方法的名称 * descriptor : 描述符 */ // 将原始方法提取出来 let fn = descriptor.value // 定义新的方法 descriptor.value = function (a: number, b: number) { // 调用原来的方法 const resul ...
7、TypeScript的模块系统和命名空间
模块导出与导入 导出 let a1 = 100 let a2 = 200 function hello() { console.log('小康你好') } export { a1, a2, hello } 没有默认导出 如果一个模块没有默认导出 // m1.ts export let obj = { x: 1 } 则在引入该模块的时候,需要使用下列一些方式来导入 // main.ts // error: 提示 m1 模块没有默认导出 import v from './m1' // 可以简单的使用如下方式 import {obj} from './m1' console.log(obj.x) // or import * as m1 from './m1' console.log(m1.obj.x) 导入 import { a1, hello } from './a' console.log(a1) hello() 模块编译 TypeScript 编译器也能够根据 ...
6、TypeScript的泛型
函数 function getVal<T>(obj: T, k: keyof T) { return obj[k] } let obj1 = { x: 1, y: 2 } let obj2 = { username: 'xiaokang', age: 18 } getVal<typeof obj1>(obj1, 'x') getVal<typeof obj2>(obj2, 'username') 泛型类 abstract class Component<T1, T2> { props: T1; state: T2; constructor(props: T1) { this.props = props; } abstract render(): string; } interface IMyComponentProps { val: number; } ...
5、TypeScript的面向对象
类 class User { // 定义成员属性 id: number username: string // 定义成员方法 postArticle(title: string, content: string) { console.log(title, content) } // 构造函数 constructor(id: number, username: string) { // 构造函数的作用:创建类的函数,当类实例化时被调用 console.log('构造函数') this.id = id this.username = username } } const user = new User(1, '小康') user.postArticle('title', 'content') 在ts中可以简化定义成员属性和赋值的夫过程 class User { postArticle(title: string, content: ...
4、TypeScript的函数
函数的标注 一个函数可以标注参数、返回值 function fn(a: string): string {}; let fn: (a: string) => string = function(a) {}; type callback = (a: string): string; interface ICallBack { (a: string): string; } let fn: callback = function(a) {}; let fn: ICallBack = function(a) {}; 可选参数和默认参数 可选参数 通过参数名后面添加 ? 来标注该参数是可选的 let div = document.querySelector('div'); function css(el: HTMLElement, attr: string, val?: any) { } // 设置 div && css( div, 'widt ...










