help to configure a proxy please
-
Hi
Im trying to configure a proxy in quasar 0.15 and i have not achivied yet, my devServer config is like this:<code>
devServer: {port: 8080, open: true, openPage: '/pmt/', proxy: { '/pmt/index.php/*': { target: 'http://localhost:80/', secure: false, logLevel: 'debug', changeOrigin: true , headers: { 'Origin': 'http://localhost:80/' }, bypass: function(req, res, proxyOptions) { console.log("bypass",req.headers) }, onProxyReq: function (proxyReq, req, res) { console.log("requested") proxyReq.setHeader('origin', 'http://localhost:80/'); }, onError(err, req, res) { console.log("Error") }, onProxyRes(proxyRes, req, res) { console.log("REspuesta") } /* , pathRewrite: { '^/pmt/': '' } */ } } },//devserver
</code>
I have noticed that the proxy somehow does not match my patterns, because i subscribed to onProxyReq function and just put a console.log and only Works with ‘/pmt’ pattern; i have used with this patterns:
/pmt/index.php/
/pmt/index.php/*
/pmt/index.php/**/**/
/pmt/index.php/**/**
/pmt/index.php/*/*/
/pmt/index.php/*/*
/pmt/index.php/LoginController/
/pmt/index.php/LoginController/*
/pmt/index.php/LoginController/validarCredenciales/
/pmt/index.php/LoginController/validarCredenciales
…
And so on…
The requested service uses axios like this:<code>
const ruta = ‘index.php/LoginController/validarCredenciales’
axios.post(payload.ruta,{data:payload.data}).then(function (response) {
if (response !== undefined && response !== null){
if(typeof payload.callback === “function”){
payload.callback.call(ctx,response);
}
}
})
.catch((error) => {
console.log(error)
return false;
})</code>
the service result always send me back a cors error:
Failed to load http://localhost/pmt/index.php/LoginController/validarCredenciales: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8080’ is therefore not allowed access.
i configured a proxy with quasar 0.14 but now i dont know what am i doing wrong, if anyone can give me a clue please…
thanks in advance
-
var app = express();
app.use(function(req, res, next) {
res.header(“Access-Control-Allow-Origin”, “*”)
res.header(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, Content-Type, Accept, Authorization”)
res.header(‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS’)
next()
})
in express this is how we set up our access control headers, I don’t know the specifics for your web server but if you add these headers to both the server that is acting as the proxy and the server which is at localhost:80 it should fix the error. -
thanks, i appreciate your comment…
In my server code i can put the headers and the requests are accepted, indeed i installed a chrome plugin for that and it works too, but i want to make it work just configuring devServe’ proxy because i had a problem with php sessions; when the server validate users credentials, it creates session variables (first i used codeigniter session library, then i changed to php normal session) but when i ask the next request, sessions did not exist (like if it were destroyed) and i thougth that maybe it was because of i haven’t configure devserver proxy like i did in quasar 0.14 when it worked (but now i cant get the proxy runs)…
I have read that maybe i must use jwt (json web tokens), but also i read that is ok to use server’s sessions… so im confused
-
i have resolved after a thousand years…
i was missunderstanding how the proxy worked; for those who has the same problem:i was setting axios.defaults.baseURL to something like this: ‘http://localhost/pmt/’ (it might not be your case); in my components the request services pointed to an url like this: ‘index.php/controller/method’ and my proxy setting was like this:
‘/pmt/index.php/’: {
target: ‘…’
…
}what i did to resolved:
- i changed axios.defaults.baseURL to ‘index.php’ (im using codeigniter framework)
- in my components services urls changed to something like ‘controller/method’
- changed my proxy to:
‘/index.php’:
{
target: ‘http://localhost:80/pmt/’,
secure: false,
logLevel: ‘debug’,
changeOrigin: true,headers: { 'Origin': 'http://localhost:80/' },
}
so when my components services ask something like controller/method, it joins to axios.defaults.baseURL in this case result in: index.php/controller/method, which the proxy match and using target property the final url is set to something like this: