简体   繁体   English

如何将vue-cli开发模式与服务器端api相结合?

[英]How to combine vue-cli development mode with server-side api?

I'm new to vue and kind of confuse here. 我是vue的新手,在这里有点困惑。

I'm using vue-cli to build a vue app, I understand I can run a development server with npm run serve which is referenced as a script in my package.json for vue-cli-service serve 我正在使用vue-cli构建一个vue应用程序,我了解我可以使用npm run serve运行开发服务器,该npm run serve在我的package.json中被引用为vue-cli-service serve的脚本

But my app need some data coming from a local node.js server. 但是我的应用程序需要一些来自本地node.js服务器的数据。 I cannot request this server from development mode because it's running on a different server. 我无法从开发模式请求此服务器,因为它在其他服务器上运行。 To make my app work I'm obligated to build for production with 为了使我的应用正常工作,我有义务使用

npm run build

Then to ask my node server to render by default the produced index.html file. 然后要求我的节点服务器默认呈现生成的index.html文件。

How could I combine development mode and my node server? 如何结合开发模式和节点服务器?

What would be the best way to make this work? 使这项工作最好的方法是什么?

Thanks a lot 非常感谢

I stumbled across this, and found the answer buried at the bottom of the comments list, so I thought I'd highlight it. 我偶然发现了这个问题,发现答案埋在了评论列表的底部,所以我想突出显示它。
This answer is taken from @Frank Provost comment, which really should be the accepted answer. 该答案来自@Frank Provost评论,它实际上应该是被接受的答案。 As mentioned in his link https://cli.vuejs.org/config/#devserver-proxy all you need to do is create/edit vue.config.js file in your (client) project root to include this: 如他的链接https://cli.vuejs.org/config/#devserver-proxy所述,您需要做的就是在(客户端)项目根目录中创建/编辑vue.config.js文件,以包括以下内容:

module.exports = {
  devServer: {
    proxy: 'http://localhost:3000' // enter dev server url here 
  }
}

Then start your dev server as usual from your server project: 然后像往常一样从服务器项目中启动开发服务器:

[server-root]$ npm run dev

And run your client project from vue-cli project 并从vue-cli项目运行您的客户端项目

[client-root]$ npm run serve

Then when you visit the client url (usually localhost:8080) all api requests will be forwarded to your dev server. 然后,当您访问客户端URL(通常是localhost:8080)时,所有api请求都将转发到您的开发服务器。 All hot module replacement still works on both client and server. 所有热模块替换仍然在客户端和服务器上均有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM