# vue+typescript+shorturl **Repository Path**: ggbhack/vue-typescript-shorturl ## Basic Information - **Project Name**: vue+typescript+shorturl - **Description**: vue+typescript+短链接 ts实战练习 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2021-03-08 - **Last Updated**: 2022-04-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # vue+typescript+shorturl #### 介绍 vue+typescript+短链接 ts实战练习 ### 学习笔记 #### 1 课程介绍 * 了解短链接的火热程度 * 知道短链接生成后,访问还可以自带广告 * 知道项目需要的技术栈 * 先使用vue大家项目 * 在使用ts改造项目 * mogoogedb+express * 使用vscode调试api 【亮点】 #### 2-8 课 * mongodb数据库的安装 * 服务端项目文件的构造 * express 服务的初始化即启动 #### 9课 mongodb 连接数据库+配置 * 将数据库链接封装起来 ``` // 创建mongodb 数据库连接 方法 const connectDB=async ()=>{ try { await mongoose.connect(dbUrl,{ useNewUrlParser:true, useUnifiedTopology:true }) console.log('mongodb connectd.') } catch (error) { console.log(error.message) process.exit(1) } } ``` * 将配置文件写道配置中 ``` { "mongodbUrl":"mongodb://127.0.0.1:27017/urls", "baseUrl":"http://www.ggb.com", "port":80 } ``` #### 10课 创建数据表结构和模型 * 学到的知识 mongodb 创建模型和表结构的方式 和其他的数据库相似,都是通过模型来对表进行操作 ``` const mongoose=require('mongoose') // 创建表结构 const urlScheme =new mongoose.Scheme({ urlCode:String, longUrl:String, shortUrl:String, date:{type:String,default:Date.now()} }) // 创建表模型 model const model = new mongoose.Model("Url",urlScheme) // 导出模型 module.exports=model ``` #### 11 API 接口路由配置 * 使用app.use() ``` // 配置路由 app.use("/",require('./routes/index')) ``` #### 短链接生成api逻辑梳理 * 获取长链接 ``` const { longUrl } = req.body; ``` * 验证长链接 ``` validUrl.isUri(longUrl) ``` * 生成url code ``` const urlCode = shortid.generate() ``` * 生成短链接 ``` shortUrl = config.get('baseUrl') + urlCode ``` * 存入数据库 ``` const url = new UrlModel({ longUrl, shortUrl, urlCode, date: new Date() }) url.save() ``` * 返回数据 #### 14.短链接api接口优化 * 根据长连接查询数据,如果存在,则返回数据 #### 15.短链接接口实现 * 获得短链接后面的code * 查询数据库-是否存在 * 存在则重定向到对应的长链接 ``` // 相关逻辑 const url = await UrlModel.findOne({ urlCode: req.params.code }) // 检测url是否存在 if (url) { res.redirect(url.longUrl) } else { res.status(404).json("Server error") } ``` #### 18 ejs模板的学习与使用 * ejs模板配置 ``` // 引入ejs const ejs = require('ejs'); // 配置ejs引擎 app.engine('html',ejs.__express) ``` * 静态资源的配置 ``` // 设置静态资源路径 app.use(express.static('./public')) ``` * 重定向到ejs模板 ``` // res.redirect(url.longUrl) res.render('index.ejs',{url:url.longUrl}) ``` #### 28课 跑马灯实现逻辑 * 实现逻辑思路 * 将标签内的数据取出,在每个文字外面都包裹一层 标签 * 定义外层标签的 样式 形成一个 由左到右 亮度从高到暗 形成反差 * 然后定时 移动 样式里面的数据,让去更新每一个标签,即可实现 #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)