为了账号安全,请及时绑定邮箱和手机立即绑定

请教各位前辈,vue-cli可以跨域拿到豆瓣的数据,为什么不能跨域拿到本地数据库的数据呢?

请教各位前辈,vue-cli可以跨域拿到豆瓣的数据,为什么不能跨域拿到本地数据库的数据呢?

慕工程0101907 2019-05-21 11:11:38
尝试用vue-cli来获取本地服务器数据库的数据但返回以下错误提示,用同样的写法却可以获取豆瓣的数据。折腾来一个晚上无法解决,希望前辈们能抽空指点!不胜感激![HPM]Erroroccurredwhiletryingtoproxyrequestfromlocalhost:8080tohttp://localhost:3000(ECONNREFUSED)(https://nodejs.org/api/errors.html#errors_common_system_errors)我的代码部分config->index.jsproxyTable:{'/api':{target:'http://localhost:3000',changeOrigin:true,pathRewrite:{'^/api':''}}src->main.jsimportVuefrom'vue'importAppfrom'./App'importVueRouterfrom'vue-router'importrouterConfigfrom'./router.config.js'//引入路由importAxiosfrom'axios'Vue.config.productionTip=false;Vue.prototype.$axios=Axios;Vue.prototype.HOST='/api';Vue.use(VueRouter);constrouter=newVueRouter(routerConfig)//启动路由/*eslint-disableno-new*/newVue({el:'#app',router,//挂载路由components:{App},//组件名template:'',})src->App.vueexportdefault{created(){varurl=this.HOSTthis.$axios.get(url,{}).then((res)=>{console.log(res.data)},(res)=>{alert(res.status)})}}后台serverconstexpress=require('express');constmysql=require('mysql');constdb=mysql.createPool({localhost:'localhost',user:'root',password:'123456',database:'blog'})constserver=express();server.use('/api',(req,res)=>{db.query(`SELECT*FROMarticles_table`,(err,data)=>{if(err){console.error(err);res.status(500).send('databaseerror').end();}else{res.send(data)}})})server.listen(3000)
查看完整描述

2 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

感谢各位前辈关注和解答,最后我用了以下办法来解决
1、安装
npminstall--save-devconcurrently
2、将package.json的script加上
"server":"nodeserver.js",
"go":"concurrently--kill-others\"npmrundev\"\"npmrunserver\""
3、输入命令
npmrungo
然后就可以了
                            
查看完整回答
反对 回复 2019-05-21
  • 2 回答
  • 0 关注
  • 615 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号