如何将Vue Server路由到Node.js后端

h5qlskok  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(113)

如何在node.js服务器上使用Vue Dev服务器?我目前正在使用npm run build从Vue创建一个构建版本,并将创建的'dist'文件夹复制到我的节点后端。还有路线

app.get('/', (req, res) => {
 res.sendFile(__dirname, 'index.html');
})

我可以访问index.html。但是我看到任何将Vue服务器服务到节点服务器的示例,以便每次都不创建构建并将创建的´/dist/文件夹复制到node.js后端。
例如,如果更改vue.config.js文件,则应该可以。
我试过这个配置:

module.exports = {
  devServer: {
    proxy: {
      '^/api': {
        target: 'http://localhost:3080',
        changeOrigin: true
      },
    }
  }
}

但是target文件中应该包含什么呢?我的node.js在端口443上运行。我也试过使用'http://localhost:443'https://<URL-FROM-Site>:443。然后启动服务器nodevue,并使用https://XXXXXXXXXXXXXXX:<PORT>/api访问vue应用程序
使用https://XXXXXXXXXXXXXXXXX:<PORT>/,我可以访问我的node.js并从dist文件夹中获取index.html文件。

shyt4zoc

shyt4zoc1#

好的,我已经在npm上安装了webpack。我需要在vue.config.js中添加一些额外的代码,对吗?现在我的vue.config.js是:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    devServer: {
      proxy: {
        '/api': {
          target: 'http://localhost:3080',
},},},},})

并将端点编辑为...app.post("/api/signup",async (req,res)=>{...,但不起作用。
我得到这个错误:Proxy error: Could not proxy request /API/signup from localhost:8080 to http://localhost:3080 (ECONNREFUSED).

相关问题