尝试用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-devconcurrently2、将package.json的script加上"server":"nodeserver.js","go":"concurrently--kill-others\"npmrundev\"\"npmrunserver\""3、输入命令npmrungo然后就可以了
添加回答
举报
0/150
提交
取消
