Javascript 电子+;Vue API请求落在应用程序上://

Javascript 电子+;Vue API请求落在应用程序上://,javascript,vue.js,electron,vue-cli,electron-builder,Javascript,Vue.js,Electron,Vue Cli,Electron Builder,我已经构建了一个Vue应用程序并添加了Electron。我曾经 在开发模式下没问题,所有API请求都落在我的vue.config.js中编写的地址上: proxy: { '^/api': { target: 'http://my-api:3000', changeOrigin: true }, }, if (process.env.WEBPACK_DEV_SERVER_URL) { // Load the

我已经构建了一个Vue应用程序并添加了Electron。我曾经

在开发模式下没问题,所有API请求都落在我的
vue.config.js
中编写的地址上:

proxy: {
        '^/api': {
            target: 'http://my-api:3000',
            changeOrigin: true
        },
    },
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} 
else {
createProtocol('app');
// Load the index.html when not in development
win.loadURL('app://./index.html');
}
proxy: {
        '^/api': {
            target: 'http://127.0.0.1:3000',
            changeOrigin: true
        },
    },
例如,Axios POST请求
/api/open\u session/
下降到
http://my-api/api/open_session
根据需要

当我构建项目时,它会创建一个
app://
协议来打开index.html文件

但它也使所有url路径以
app://
开头,包括API请求

My
background.js

proxy: {
        '^/api': {
            target: 'http://my-api:3000',
            changeOrigin: true
        },
    },
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} 
else {
createProtocol('app');
// Load the index.html when not in development
win.loadURL('app://./index.html');
}
proxy: {
        '^/api': {
            target: 'http://127.0.0.1:3000',
            changeOrigin: true
        },
    },

我希望这些路径指向我的API,同时像往常一样打开我的所有文件(通过应用程序协议)

好吧,这是一段较长的时间,我自己处理。然而,我在一些论坛上找到了一个答案,是为那些正在为同一问题挣扎的人准备的:

首先,我修改了我的
vue.config.js

proxy: {
        '^/api': {
            target: 'http://my-api:3000',
            changeOrigin: true
        },
    },
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} 
else {
createProtocol('app');
// Load the index.html when not in development
win.loadURL('app://./index.html');
}
proxy: {
        '^/api': {
            target: 'http://127.0.0.1:3000',
            changeOrigin: true
        },
    },
然后,我在
main.js中做了一些更改-添加了一个会话变量:

const sesh = session.defaultSession.webRequest.onBeforeSendHeaders({
 urls: ['*://*/*']
}, (details, callback) => {
// eslint-disable-next-line prefer-destructuring
details.requestHeaders.Host = details.url.split('://')[1].split('/')[0]
callback({
  requestHeaders: details.requestHeaders
})
})
它定义了调用请求时应用程序的行为。此外,我还为webPreferences添加了一个会话值:

const win = new BrowserWindow({
 width: 1500,
 height: 700,
 title: "Title",
 webPreferences: {
   session: sesh,
   nodeIntegration: true,
   webSecurity: false
 }
})
最后,通过应用程序协议加载my index.html

createProtocol('app');
win.loadURL('app://./index.html');
结果,我的所有请求都被重定向到我的服务器


请原谅我不知道源代码,如果代码的作者正在阅读本文,您肯定可以在注释中标记自己:)

好吧,这是一段较长的时间,我自己处理了。然而,我在一些论坛上找到了一个答案,是为那些正在为同一问题挣扎的人准备的:

首先,我修改了我的
vue.config.js

proxy: {
        '^/api': {
            target: 'http://my-api:3000',
            changeOrigin: true
        },
    },
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} 
else {
createProtocol('app');
// Load the index.html when not in development
win.loadURL('app://./index.html');
}
proxy: {
        '^/api': {
            target: 'http://127.0.0.1:3000',
            changeOrigin: true
        },
    },
然后,我在
main.js中做了一些更改-添加了一个会话变量:

const sesh = session.defaultSession.webRequest.onBeforeSendHeaders({
 urls: ['*://*/*']
}, (details, callback) => {
// eslint-disable-next-line prefer-destructuring
details.requestHeaders.Host = details.url.split('://')[1].split('/')[0]
callback({
  requestHeaders: details.requestHeaders
})
})
它定义了调用请求时应用程序的行为。此外,我还为webPreferences添加了一个会话值:

const win = new BrowserWindow({
 width: 1500,
 height: 700,
 title: "Title",
 webPreferences: {
   session: sesh,
   nodeIntegration: true,
   webSecurity: false
 }
})
最后,通过应用程序协议加载my index.html

createProtocol('app');
win.loadURL('app://./index.html');
结果,我的所有请求都被重定向到我的服务器

请原谅我不知道源代码,如果代码作者正在阅读本文,您肯定可以在注释中标记自己:)