Having a bit of a problem with Grunt server using proxies.
I have everything working fine, however, when I submit a post request I get a 200 success from my proxy.
Then with each subsequent use of that proxy I get a 504 Gateway Timeout.
For instance if I post to student-minors proxy it goes through fine.
Every time after that I get a 504 when I try to do something else with that proxy.
I can use the other proxies with a get, I can get terms and courses.
Seems like something is either hanging, throwing an error of some kind, but I can't see it.
Any insight would be appreciated.
Here is a snippet of the connect server with the proxy stuff
// Define the configuration for all the tasks
grunt.initConfig({
connect: {
rules: [
{from: '^/poc-proxy/(.*)$', to: '/authorizations/$1'},
{from: '^/service/apis/terms/(.*)$', to: '/terms/$1'},
{from: '^/service/apis/courses/(.*)$', to: '/courses/$1'},
{from: '^/service/apis/studentMinors/(.*)$', to: '/studentMinors/$1'}
],
server: {
options: {
port: 9000,
base: 'dev',
hostname: 'localhost',
middleware: function (connect, res,options, middlewares) {
return [
rewriteRules,
serveStatic('./dev'),
require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware()
]
}
},
proxies: [{
context: '/authorizations', //REQUIRED! Must start with a '/' should not end with a '/'
host: 'authorizations.com', //REQUIRED! Should not contain 'http://' or 'https://'
changeOrigin: true,
https: true,//Optional, defaults to false
headers: {//Optional.
'Access-Control-Allow-Origin': '*'
}
},
{
context: '/terms', //REQUIRED! Must start with a '/' should not end with a '/'
host: 'terms.com', //REQUIRED! Should not contain 'http://' or 'https://'
changeOrigin: true,
https: true,//Optional, defaults to false
headers: {//Optional.
'Access-Control-Allow-Origin': '*'
}
},
{
context: '/courses', //REQUIRED! Must start with a '/' should not end with a '/'
host: 'courses.com', //REQUIRED! Should not contain 'http://' or 'https://'
changeOrigin: true,
https: true,//Optional, defaults to false
headers: {//Optional.
'Access-Control-Allow-Origin': '*'
}
},
{
context: '/studentMinors', //REQUIRED! Must start with a '/' should not end with a '/'
host: 'minors.com', //REQUIRED! Should not contain 'http://' or 'https://'
https: true,
changeOrigin: true,
headers: {//Optional.
'Access-Control-Allow-Origin': '*'
}
}
]
}
},
Changed the server options.
server: {
options: {
port: 9000,
base: 'dev',
hostname: 'localhost',
middleware: function (connect, res,options, middlewares) {
return [
rewriteRules,
serveStatic('./dev'),
//require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware()
require('grunt-connect-proxy/lib/utils').proxyRequest
]
}
},
Changed the task run
grunt.registerTask('serve', 'start the server and preview your app', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'browserSync:dist']);
}
grunt.task.run([
'configureRewriteRules',
//'setupProxies:server',
'configureProxies:server',
'connect:server',
'clean:server',
'wiredep',
'concurrent:server',
'postcss:dev',
'copy:dev',
//'browserSync:livereload', //Browsersync a proxy servers conflict with CORS
'watch'
]);
});
Related
I have a cors issue in my development with vue3 & vite, so I create a proxy config in my vite.config.js
import { defineConfig } from 'vite'
import vue from '#vitejs/plugin-vue'
import { resolve } from 'path'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'#': resolve(__dirname, 'src'),
},
},
server: {
proxy: {
"/api": {
target: "https://###.com",
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^\/api/, ""),
},
}
}
})
when I use in my app my method:
createPayment() {
const url = "/api/webhook/###";
let formData = new FormData();
formData.append("firstName", this.first_name);
formData.append("lastName", this.last_name);
formData.append("email", this.email);
formData.append("phone", this.phone);
formData.append("cardNumber", this.c_number);
formData.append("cardExpiration", this.c_EXP);
formData.append("cardCCV", this.c_CVC);
const request = new Request(url, {
method: "POST",
body: formData,
headers: {
accept: 'application/json',
contentType: "application/json;charset=UTF-8",
AccessControlAllowOrigin: '*'
},
});
fetch(request)
.then(result => console.log(result))
.catch(error => console.log('error', error));
},
The Post all ways send from localhost, I can't understand why?
I make the config for change '/api' to 'https://###.com' ?
My Log:
Response {type: 'basic', url: 'http://localhost:3000/api/webhook/####', redirected: false, status: 404, ok: false, …}
I have a method that should return a response from another server. I use request-promise and put the URL in the options object.
As you can see in the code below, all is in good shape, but when I send the request, it returns 404 - resource not found.
When I change the request(options) method with request("https://api.quickpay.net/payments"), I get a positive answer from the server -- it tells me to add headers and so forth, which is positive.
public requestNewQuickpayPayment(order_id: String, currency : String, callback: Function) {
var options = {
method: 'POST',
uri: 'https://api.quickpay.net/payments',
form:{
order_id : "order123",
currency : "dkk"
},
headers: {
"Content-Type" : "application/json",
'Accept-Version': 'v10'
},
json: true
};
request(options).then((response:any)=>{
console.log(response);
return response;
}).catch((error:any)=>{
console.log(error);
return error;
}).finally(()=>{
console.log("done");
})
}
Something from the console
Request {
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
method: 'POST',
uri: [Url],
transform2xxOnly: true,
headers: [Object],
readable: true,
writable: true,
explicitMethod: true,
_qs: [Querystring],
_auth: [Auth],
_oauth: [OAuth],
_multipart: [Multipart],
_redirect: [Redirect],
_tunnel: [Tunnel],
_rp_resolve: [Function],
_rp_reject: [Function],
_rp_promise: [Promise],
_rp_callbackOrig: undefined,
callback: [Function],
_rp_options: [Object],
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function: RP$callback],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'api.quickpay.net',
body: 'order_id=asdasdasd¤cy=dkk',
path: '/payments',
_json: true,
httpModule: [Object],
agentClass: [Function],
agent: [Agent],
_started: true,
href: 'https://api.quickpay.net/payments',
req: [ClientRequest],
ntick: true,
response: [Circular],
originalHost: 'api.quickpay.net',
originalHostHeaderName: 'host',
responseContent: [Circular],
_destdata: true,
_ended: true,
_callbackCalled: true },
toJSON: [Function: responseToJSON],
caseless: Caseless { dict: [Object] },
body: '404 Not Found' } }
What is wrong here? The path to the ressource is checked many times - nothing is wrong there....
For api.quickpay.net, 404 Not Found does not mean the URI is not recognized, but indicating invalid request body. It has nothing to do with whether the URI is stated in options object, or stated as string parameter of request().
Here is a simple experiment. The code below would return "positive" result, warning missing headers ({"error":"Accept-Version http header is required"}), which indicates that the URI is "recognized":
request({
method: 'POST',
uri: 'https://api.quickpay.net/payments'
}, function(err, res, body) {
console.log(body);
});
However, after the missing Accept-Version header is added, we get 404 Not Found:
request({
method: 'POST',
uri: 'https://api.quickpay.net/payments',
headers: {
'Accept-Version': 'v10'
}
}, function(err, res, body) {
console.log(body);
});
Thus, in order to make the API call work, you need to make the HTTP request valid (following to the document).
Problem:
My app runs on digitalocean droplet with multiple domains:
proxy: {
domains: 'example.com,www.example.com',
ssl: {
letsEncryptEmail: '#'
}
}
Sometimes, for about half an hour the https://example.com fails to load completely but indirect links like https://example.com/about works fine.
Tried:
fiddling with nginx option:
nginxServerConfig: './nginx.conf',
Any attempts with it failed loading page completely
Mup.js file:
module.exports = {
servers: {
one: {}
},
app: {
deployCheckWaitTime: 300,
name: 'example',
path: '../',
buildOptions: {
serverOnly: true,
},
env: {
ROOT_URL: 'https://example.com',
MONGO_URL: 'mongodb://mongodb:27017/example',
},
docker: {
image: 'abernix/meteord:node-8.4.0-base',
args: ['--link=mongodb:mongodb'],
},
enableUploadProgressBar: true
},
proxy: {
domains: 'example.com,www.example.com',
ssl: {
letsEncryptEmail: '#'
}
}
};
Turns out, that problem lied in mailgun.
The mailgun DNS records didn't match v=spf1 include:eu.mailgun.org ~all thus those mails weren't authorized and whenever mail was sent through the system it was tripping domain provider to refresh it's DNS.
I solved this issue by setting up a permanent redirect for www through my domain settings.
When I try using Grunt to create a server for me it quickly shuts down and doesn't give me a change to go to my browser and test it.
This is my entire Grunt file:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.initConfig({
connect: {
serve: {
options: {
port: 8081,
base: '/',
hostname: '*',
debug: true
}
}
}
});
grunt.registerTask('default', ['connect']);
}
When I run it, it works without errors:
C:\Users\Imray\my-sandbox>grunt
Running "connect:keepalive" (connect) task
Started connect web server on http://0.0.0.0:8000
Running "connect:serve" (connect) task
Started connect web server on http://0.0.0.0:8081
You have the keepalive settings dedicated for this :
grunt.initConfig({
connect: {
serve: {
options: {
keepalive: true,
port: 8081,
base: '/',
hostname: '*',
debug: true
}
}
}
});
I'm trying to allow cors on grunt connect task that hosts the angular app. *I'm using angular yeoman generator. Here is what i've tried so far:
connect: {
options: {
port: 9004,
hostname: 'localhost',
livereload: 35728,
middleware: function(connect, options) {
return [
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
return next();
},
connect.static(require('path').resolve(yeomanConfig.app))
];
}
},
When i load localhost:9004 i want to have access-control-allow-origin:* in headers