![](https://img.haomeiwen.com/i6560153/d89eef0980b5568d.png)
创建Server Folder. 然后分屏左边启动Client 右边进入Server
![](https://img.haomeiwen.com/i6560153/8f820a643465ab4f.png)
npm init -f 可以创建一个Package.json 在Server Folder里
install nodemon
![](https://img.haomeiwen.com/i6560153/a498101d6b7b4cf4.png)
往Script 里添加 start 和 lint. 这样npm run start/ npm run lint 就是启动这个
![](https://img.haomeiwen.com/i6560153/ca6419874962cd3f.png)
在Server里src创建app.js
![](https://img.haomeiwen.com/i6560153/920a205e76f60c7a.png)
Npm install body-parser, express, cors, Morgan
![](https://img.haomeiwen.com/i6560153/0b04b3c985d852ef.png)
此时的Server/app.js 大概
![](https://img.haomeiwen.com/i6560153/28404eabb57e89bd.png)
现在转入Vue.js 部分
创建一个Services Folder 创建api.js 和 authentication.js
![](https://img.haomeiwen.com/i6560153/5e473d1c87b0120e.png)
![](https://img.haomeiwen.com/i6560153/b6d351e39ca0fbab.png)
Router部分定义在index.js 这里我们设置一个route to register.
![](https://img.haomeiwen.com/i6560153/79d4c618fb0a68ef.png)
在component Folder里 创建register component.
这里就是纯vue语法了, 比较重要的有v-model, which is a two direction bindings.
@click = "function name"
![](https://img.haomeiwen.com/i6560153/664d0085f5c4ea69.png)
--------------------------------------------------------------
实现的几个牛逼功能:
1. 登录
![](https://img.haomeiwen.com/i6560153/b61032892c79caf1.png)
![](https://img.haomeiwen.com/i6560153/06daee212ebcd9eb.png)
这里用到了一个two way bindings. 用户输入的value会进入component里的变量里,然后我们用this.email, this.password来登录
调用Store的方式 this.$store....
![](https://img.haomeiwen.com/i6560153/3091e661cf517b76.png)
这里最核心的就是调用AuthenticationService以及setToken,setUser 和router 切换。Login成功跳转到name位'songs'的component
![](https://img.haomeiwen.com/i6560153/f3d85c27b9a967a3.png)
vuex 跟react-redux很像, 一个central store:
定义Store的property
state里面表示Store里存放的property
![](https://img.haomeiwen.com/i6560153/84766516bddef027.png)
在Server端:
先在Routes里定义:
![](https://img.haomeiwen.com/i6560153/f9a3e5bef4ce0cc0.png)
定义AuthenticationController.js
我们可以从req.body里提取出用户传入的data。
![](https://img.haomeiwen.com/i6560153/219dcc2b9f724ec1.png)
2. 搜索
![](https://img.haomeiwen.com/i6560153/67c688603e83b2eb.png)
使用lodash里的debounce方法,使搜索时候不会每个字都去搜索,而是等用户打完字几秒后开始。
***route.query 设置为this.search也就是用户输入的值。search也是two way binding
![](https://img.haomeiwen.com/i6560153/443e94527bf46502.png)
3. Bookmarks
首先在model 里:
![](https://img.haomeiwen.com/i6560153/345cd828ac1a4b2c.png)
在bookmarks Controller里, 这里userId是从req.user.id里取出来的原因是我们使用了middleware 往request里添加了一个新的field
req.user = USER
![](https://img.haomeiwen.com/i6560153/c151f14201744537.png)
在Policy Folder的IsAuthenticate.js里
![](https://img.haomeiwen.com/i6560153/2baa44a1589eeae3.png)
Client side:
Script里面的逻辑其实很简单,就是copy paste一个data-table和pagination.
![](https://img.haomeiwen.com/i6560153/427d45bfe0a6239f.png)
重点在于: page一呈现出来的时候立马使用mounted 然后取出bookmarks data。 然后data会被以Table的方式呈现。
![](https://img.haomeiwen.com/i6560153/735d258c4a9691c5.png)
触发Favorite的问题
![](https://img.haomeiwen.com/i6560153/6c5727690c2a02b2.png)
![](https://img.haomeiwen.com/i6560153/0208c5fa0367aee1.png)
![](https://img.haomeiwen.com/i6560153/3d2fc01a50658150.png)
![](https://img.haomeiwen.com/i6560153/7fb5676a41025eb6.png)
4. 历史记录
历史记录基本跟Bookmarks是一样的原理
![](https://img.haomeiwen.com/i6560153/e5fe3ac03afcac54.png)
要注意的是,我们在详细浏览一个music的时候,要在mounted() 里post一个记录到SongHistoryService. 为什么只传入songId?因为 User info记录在persistent的cache里了?
![](https://img.haomeiwen.com/i6560153/aa64829582a60680.png)
5. Youtube
vue-youtube-embe
其实很简单,只需要在数据库里存放Youtube链接的link就好了。
![](https://img.haomeiwen.com/i6560153/a13ddec715925f3a.png)
:video-id 那里放置 outer component传入的props: Youtube-id
![](https://img.haomeiwen.com/i6560153/c52bbe6500d862c2.png)
6. Passport Authenticate
![](https://img.haomeiwen.com/i6560153/a247ec74e1a6fc6a.png)
Key is the token!!! 我们把这个token存放在store里。【突然意识到这样会不会影响到高并发?还是说因为VueJs是前端所以每个人的浏览器里有一个unique的Store?】
![](https://img.haomeiwen.com/i6560153/d8b9eb73da603986.png)
Model里的user.js
这个可以算做一个User Schema,专门用来造user。
![](https://img.haomeiwen.com/i6560153/d62b72981eab5474.png)
在AuthenticationController 里定义:
这里主要是给新建的user sign 一个token。
![](https://img.haomeiwen.com/i6560153/8ab887ea19c948f6.png)
看,这里的user可以调用comparePassword的方法。
![](https://img.haomeiwen.com/i6560153/9fc47093116b4efa.png)
res.send(..) 是从server side send back to client side.
这里client side把response里的Token存入Store里。
![](https://img.haomeiwen.com/i6560153/09df624c433de54a.png)
![](https://img.haomeiwen.com/i6560153/c34a609e5ed0d407.png)
passport.js
JWT (JSON Web Token)
![](https://img.haomeiwen.com/i6560153/7e24d5ec2b27bf1f.png)
划重点:
vuetify Icon
middleware injection
axios
export, require
![](https://img.haomeiwen.com/i6560153/d17b8a40e485a182.png)
![](https://img.haomeiwen.com/i6560153/ab2839a90985fad5.png)
Async await
Store
watch
Lodash
mounted
computed 我们需要知道他和Methods的区别。。。
效果其实差不多,但是主要是Performance原因
![](https://img.haomeiwen.com/i6560153/f18cebfd397ebea5.png)
![](https://img.haomeiwen.com/i6560153/ac8e9e8605c6233c.png)
![](https://img.haomeiwen.com/i6560153/09208dcd55581a54.png)
...mapState(){}
![](https://img.haomeiwen.com/i6560153/e5634a0905d4e9b4.png)
![](https://img.haomeiwen.com/i6560153/50910680edb8cd8a.png)
Methods需要被Explicitly 调用才可以,
比如
![](https://img.haomeiwen.com/i6560153/2a19bf5078c31956.png)
Computed会自动update DOM的值 whenever sth changed.
Example: https://codepen.io/Kradek/pen/gROQeB?editors=1010
![](https://img.haomeiwen.com/i6560153/1e8e9d220ab45cfa.png)
在Vue.js里import
位置是在vue file里的script区域:
import '../../../node_modules/vue-material-design-icons/styles.css'
SQL数据库NodeJs的创建:
网友评论