基于vue+百度音乐api +express + mongodb + elementUi定义自己的音乐播放器(五)

接下来实现将歌曲信息添加到数据库

1.我们先要定义好结构,在我们之前通过express xxx构建的项目中定义一个models文件夹,创建一个music.js文件,写入以下代码

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// 定义表结构
var musicObj = new Schema({
    song_id: String,
    author: String,
    title: String,
    hot: String
});
module.exports = mongoose.model('musics',musicObj);//暴露出去

2.在我们的routes文件夹下创建一个music.js文件,写入以下代码

var express = require('express');
var Router = express.Router();
//引入刚刚暴露出来的表模型
var musicModel = require('../models/music');
//定义添加到收藏的接口
Router.post('/collect',function(req,res,next){
      // 这里是post请求,使用req.body来获取前端传过来的数据
      let musiCollect = new musicModel({
        song_id: req.body.song_id, 
        author: req.body.author,
        title: req.body.title,
        hot: req.body.hot
      });
      // 保存
      musiCollect.save(function(err,doc){
        if(err) {
            res.json({
                states: 0,
                msg: err.message
            });
        }else {
            res.json({
                states: 1,
                msg: '保存成功'
            });
        }
      });
});
//查询收藏的数据
Router.post('/list',function(req,res,next){
    musicModel.find({},function(err,doc){
      if(err) {
          res.json({
              states: 0,
              msg: err.message
          });
      }else {
          res.json({
              states: 1,
              msg: doc //查询到的数据,以json格式返回
          });
      }
    });
});
module.exports = Router;//暴露出路由

3.在app.js里面引入暴露出的路由var musicRouter = require('./routes/music');
4.使用该路由app.use('/music', musicRouter);
5.这样我们就可以在前端通过/music/collect来请求我们的添加收藏接口
6.这里需要注意的是前端的端口是8080,后端是3000所以存在跨域问题,在vue项目的config文件夹下有个index.js文件,将反向代理加上

//这里的意思是凡是以/music开头的接口都会指向3000的端口
 proxyTable: {
      '/music/*':{
        target: 'http://localhost:3000'
      }
    },

7.注意的是这里的配置文件修改后的重启服务
8.先引入import axios from "axios",这里不知道的可以先了解下vue的基础知识,你也可以用vuex来封装这些请求,这里就不做介绍

// 添加到收藏
            addMusic(item) {
                    let datas  = {
                        song_id: item.song_id,
                        author: item.author,
                        title: item.title,
                        hot: item.hot
                    }
                axios.post('/music/collect',datas).then(data=>{
                    this.$message({
                      message: '收藏成功',
                      type: 'success'
                    });  
                 })
            },

9.如果你使用mongoVUE就可以看到数据已经保存成功

22.png

这样首页的列表和查询接口实现完了,添加到数据库也实现了,下面是music.vue的全部代码





中间引入了一个hover-effects.css


.container{margin: 0 auto; max-width: 1160px;}
h2{color: #fff; float: left; width: 100%; font-size: 24px; font-weight: 400; text-align: center; padding: 50px 0 40px; position: relative; z-index: 50;}
h2 span{position: relative; padding-bottom: 10px;}
h2 span:after{content: ""; width: 50%; height: 3px; background-color: #fff; position: absolute; left: 25%; bottom: 0;}
*{margin: 0; padding: 0; box-sizing: border-box;}
.full-length img{max-width: 100%; vertical-align: middle;}
.full-length{width: 100%; float: left; padding-bottom: 30px;overflow: hidden;padding-left: 5%;}
ul{margin: 0 -1.5%;}
.geBan li[data-v-7c5ce25b] {
    float: left;
    width: 22%;
    margin: 10px 1%;
    list-style: none;
}

h3{font-size: 14px; margin: 5px 0 10px;}
p{font-weight: 300; line-height: 20px; font-size: 14px; margin-bottom: 15px;}
.btn{display: inline-block; padding: 5px 10px; font-size: 14px; color: #fff; border: 2px solid #4d92d9; background-color: #4d92d9; text-decoration: none; transition: 0.4s;}
.btn:hover{background-color: transparent; color: #4d92d9; transition: 0.4s;}
.text-desc{position: absolute; left: 0; top: 0; background-color: #fff; height: 100%; opacity: 0; width: 100%; padding: 20px;}
/*= Reset CSS End
================= *

/* effect-1 css */
.port-1{float: left; width: 100%; position: relative; overflow: hidden; text-align: center; border: 4px solid rgba(255, 255, 255, 0.9);}
.port-1 .text-desc{opacity: 0.9; top: -100%; transition: 0.5s; color: #000; padding: 55px 20px 20px;}
.port-1 img{transition: 0.5s;}
.port-1:hover img{transform: scale(1.2);}

.port-1.effect-1:hover .text-desc{top: 0;}

.port-1.effect-2 .text-desc{top: auto; bottom: -100%;}
.port-1.effect-2:hover .text-desc{bottom: 0;}

.port-1.effect-3 .text-desc{top: 50%; left: 50%; width: 0; height: 0; overflow: hidden; padding: 0;}
.port-1.effect-3:hover .text-desc{width: 100%; top: 0; left: 0; height: 100%; padding: 15px 20px 20px;}

/*= Media Screen CSS
==================== */
@media only screen and (max-width: 1090px){
    ul{width: 340px; margin: 0 auto;}
    li{width: 100%; margin: 20px 0;}
    .port-5.effect-1 {z-index: 19;}
}

@media only screen and (max-width: 360px){
    ul{width: 300px;}
    .port-1 .text-desc, 
    .port-1.effect-3:hover .text-desc, 
    .port-3.effect-1 .text-desc, 
    .port-3.effect-3 .text-desc,
    .port-4.effect-1 .text-desc,
    .port-4.effect-2 .text-desc,
    .port-4.effect-3 .text-desc, .port-8 .text-desc{padding: 20px;}
    .text-desc{padding: 7px;}
    .port-5.effect-1 .text-desc{padding: 13px 20px 20px 90px;}
    .port-5.effect-2 .text-desc{padding: 10px;}
    .port-5.effect-3 .text-desc{padding: 16px 90px 20px 20px;}
    .port-6.effect-1 .text-desc .btn, 
    .port-6.effect-2 .text-desc .btn,
    .port-6.effect-3 .text-desc .btn,
    .port-7.effect-1 .text-desc .btn,
    .port-7.effect-2 .text-desc .btn,
    .port-7.effect-3 .text-desc .btn,
    .port-8.effect-3 .text-desc .btn{display: none;}
    .port-6.effect-2 .text-desc{padding: 20px 120px 20px 20px;}
    .port-6.effect-3 .text-desc{padding: 75px 20px 10px;}
    .port-7.effect-1 .text-desc{padding: 12px 10px;}
    .port-8.effect-3 .text-desc{padding: 28px 70px 20px;}
}
/*= Media Screen CSS End
======================== */

.pricingTable{
    padding: 100px 0 20px;
    border: 1px solid #ddd;
    text-align: center;
    position: relative;
    transition: all 0.5s ease 0s;
}
.pricingTable .title{
    width: 100%;
    padding: 10px 0;
    margin: 0;
    background: #f7f2f0;
    border: 1px solid #ddd;
    font-size: 22px;
    font-weight: 800;
    color: #25283d;
    text-transform: uppercase;
    position: absolute;
    top: 30px;
    left: -15px;
    transition: all 0.5s ease 0s;
}
.pricingTable:hover,
.pricingTable:hover .title{ border: 1px solid #25283d; }
.pricingTable .title:after{
    content: "";
    border-top: 15px solid #d2d2d2;
    border-left: 15px solid transparent;
    border-bottom: 15px solid transparent;
    position: absolute;
    bottom: -30px;
    left: -1px;
    transition: all 0.5s ease 0s;
}
.pricingTable:hover .title:after{ border-top: 15px solid #000; }
.pricingTable .price-value{
    font-size: 50px;
    color: #25283d;
    margin-bottom: 40px;
}
.pricingTable .month{
    display: block;
    font-size: 14px;
    color: #bb69a2;
    line-height: 0;
    text-transform: uppercase;
}
.pricingTable .pricing-content{
    list-style: none;
    padding: 0;
    margin: 0 0 30px 0;
}
.pricingTable .pricing-content li{
    font-size: 17px;
    color: #848484;
    line-height: 45px;
    border-bottom: 1px solid #ddd;
}
.pricingTable .pricing-content li:nth-child(odd){ background: #f7f2f0; }
.pricingTable .pricing-content li:first-child{ border-top: 1px solid #ddd; }
.pricingTable .pricingTable-signup{
    display: inline-block;
    font-size: 18px;
    font-weight: 600;
    color: #25283d;
    text-transform: uppercase;
    position: relative;
    transition: all 0.3s ease 0s;
}
.pricingTable .pricingTable-signup:hover{ color: #bb69a2; }
.pricingTable .pricingTable-signup span{
    display: block;
    padding: 7px 30px;
}
.pricingTable .pricingTable-signup:before,
.pricingTable .pricingTable-signup:after,
.pricingTable .pricingTable-signup span:before,
.pricingTable .pricingTable-signup span:after{
    content: "";
    background: #bb69a2;
    position: absolute;
    top: 0;
    left: 0;
    transition: all 0.15s ease-in-out 0s;
}
.pricingTable .pricingTable-signup:before,
.pricingTable .pricingTable-signup:after{
    width: 2px;
    height: 0;
}
.pricingTable .pricingTable-signup span:before,
.pricingTable .pricingTable-signup span:after{
    width: 0;
    height: 2px;
    transition-delay: 0.15s;
}
.pricingTable .pricingTable-signup:after{
    top: auto;
    left: auto;
    right: 0;
    bottom: 0;
}
.pricingTable .pricingTable-signup span:before{
    right: 0;
    left: auto;
}
.pricingTable .pricingTable-signup span:after{
    top: auto;
    bottom: 0;
}
.pricingTable .pricingTable-signup:hover:before,
.pricingTable .pricingTable-signup:hover:after{
    height: 100%;
    transition-delay: 0.15s;
}
.pricingTable .pricingTable-signup:hover span:before,
.pricingTable .pricingTable-signup:hover span:after{
    width: 100%;
    transition-delay: 0s;
}
@media only screen and (max-width: 990px){
    .pricingTable{ margin-bottom: 30px; }
}              

下节将介绍收藏页的数据展示以及歌词滚动,进度条样式控制等

你可能感兴趣的:(基于vue+百度音乐api +express + mongodb + elementUi定义自己的音乐播放器(五))