use electron, to package projects into offline applications. Use the file protocol to read static resources locally. However, if the ajax request uses a relative path, after packaging, it will go directly to the root directory.
// main.js
const winURL = process.env.NODE_ENV === "development"
? `http://localhost:8080`
: `file://${__dirname}/index.html`
mainWindow.loadURL(winURL)
// api.js
export function loginByUsername(username, password) {
const data = {
username,
password
}
return request({
url: "/ntwechat/login/login",
method: "get",
params: {
name: username,
password: password
}
})
}
at this time, the request url of network is file:/D:/ntwechat/login/login?name=nt&password
. You need to provide ajax with a complete url path, such as http: www.xxx.com/ntwechat/login/login
,
but this is too much work. I want to know if electron intercepts url.
found electron"s session module by searching
const {session} = require("electron")
session.defaultSession.webRequest.onBeforeRequest(filter, (details, callback) => {
details.url = details.url.replace(/.*\/ntwechat\//ig, "http://localhost:8080/ntwechat/")
callback({cancel: false, url: details.url});
})
but I still can"t change the request url of the request
is there any way to solve this problem easily?