使用node创建一个服务器,运行vue打包以后的文件
原理就是使用node里的express框架,搭建一个服务器,然后访问dist文件夹里的文件
prod.server.js
1 var express = require('express') 2 var compression = require('compression') 3 var config = require('./config/index') 4 var axios = require('axios') 5 6 var port = process.env.PORT || config.build.port 7 8 var app = express() 9 10 var apiRoutes = express.Router() 11 12 apiRoutes.get('/getDiscList', function (req, res) { 13 var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg' 14 axios.get(url, { 15 headers: { 16 referer: 'https://c.y.qq.com/', 17 host: 'c.y.qq.com' 18 }, 19 params: req.query 20 }).then((response) => { 21 res.json(response.data) 22 }).catch((e) => { 23 console.log(e) 24 }) 25 }) 26 27 apiRoutes.get('/lyric', function (req, res) { 28 var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg' 29 30 axios.get(url, { 31 headers: { 32 referer: 'https://c.y.qq.com/', 33 host: 'c.y.qq.com' 34 }, 35 params: req.query 36 }).then((response) => { 37 var ret = response.data 38 if (typeof ret === 'string') { 39 var reg = /^\w+\(({[^\(\)]+})\)$/ 40 var matches = response.data.match(reg) 41 if (matches) { 42 ret = JSON.parse(matches[1]) 43 } 44 } 45 res.json(ret) 46 }).catch((e) => { 47 console.log(e) 48 }) 49 }) 50 51 app.use('/api', apiRoutes) 52 53 app.use(compression()) 54 55 app.use(express.static('./dist')) 56 57 module.exports = app.listen(port, function (err) { 58 if (err) { 59 console.log(err) 60 return 61 } 62 console.log('Listening at http://localhost:' + port + '\n') 63 })
这个是Vue 2.0 高级实战-开发移动端音乐 WebApp里的课程