Fetch JSON from same Server (localhost) but different Virtual Host - javascript

I'm having quite a basic issue I suppose. I've set up CraftCMS to be reachable via Apache Virtual Host http://craftapi and am using Element API with routes such as http://craftapi/projects?page=1 etc.
On a separate Virtual Host http://mysite I've set up a react-static install which should fetch the JSON using Axios from http://craftapi/projects?page=1 but it throws after Importing routes from directory... with:
Error: socket hang up
Am I not allowed to fetch from a different domain? Even if it is hosted on the same server?
Thanks

Related

Graphql server - Can't access via IP address url (localhost works fine)

I would like to link my Expo(React Native) app to local GraphQL backend,
http://localhost:4000/ <- Works fine
http://192.168.X.X:4000/ <- This doesn't work
I do see the Apollo Studio page, but it comes up with the below message:
Unable to reach server
Network requests from Studio on HTTPS to your HTTP endpoint are not secure,
so we cannot introspect your endpoint.
https://studio.apollographql.com/sandbox/explorer
Apollo Studio has a special exception that allows it to communicate with "localhost" over http, but all other endpoints must be over https.
If you (like me) are running virtual machines on your local machine and need to use Apollo Studio, the only solutions are to make the connection https somehow, or forward a port in your host OS so that you can access it via localhost. How to do THAT depends on your OS.

Calling HTTP Node server inside HTTPS instance, SSL error

I'm trying to connect to a socket.io inside a Node server from a React client.
Both, the React client and the instance that the Node server is located (it's a microservice, there's also a Java container running there, both within a separated docker container) have https protocols. React client is inside a s3 bucket.
The thing is, Node server is HTTP only, has no certificate, and it's causing the request to the socket.io to fail.
The connection happens without any trouble inside de develop EC2, which is not HTTPS, also running normally with localhost.
Cors are enabled.
Is there a way, without having to turn Node serve into HTTPS, to make this requests not return SSL erros?
If not, what is the easiest way to turn it into a HTTPS server to run along with the Java server?
Thanks a lot!
A very short answer. Try using nginx as WebSocket proxy for you socket.io server.
Here are couple links:
nginx as WebSocket proxy
SSL configuration to make it an https
Socket.io official configuration

Accessing Rest API for kafka schema registry using server on same domain

I have kafka set up on my machine with schema registry enabled.
I am also writing a Flask application on my same machine.
I need to access the Rest API i.e.
localhost:8081/schemas/ids/1
From
localhost:5000
How do I achieve this?
I am using latest version of kafka and schema registry.. and have tried using JavaScript CORS request
EDIT
Here is the properties file
Here is the code
Here is the error
Here is the logs
Add the following config parameters to your etc/schema-registry/schema-registry.properties file to enable CORS
access.control.allow.methods=GET,POST,PUT,OPTIONS
access.control.allow.origin=*
Full documentation for config params is here http://docs.confluent.io/current/schema-registry/docs/config.html

WebPack: Accessing Node Environment Variables in client code

I'm working on a project using Webpack to package my client side JavaScript and CSS. It launches a server so I can do hot reloading and other neat tricks. So when I'm debugging my application, the webpack server is running at localhost:3000. I am also using nodemon to launch another web server to host my API calls. It obviously can't run on the same port, so I have to launch it on port 3002.
I have set a node environment variable that tells my api what port it should host on. I need to somehow gain access to that same environment variable in my client script so my ajax calls know what port they need to be calling.
Before I started using webpack, I was hosting my api and my client code from the same port and I could just make api calls like this 'controller/action'. Now that I have them hosted in essentially two different domains, I need to tell my api to call a fully qualified url including the port. ie: 'host:port/controller/action'. I understand that I'll also need to configure CORS on my API server as well.
When I push this to production, I will be hosting both client files and API calls from the same domain once again, so I will be able to continue making relative api calls 'controller/action'. So I need to gain access to the environment variables from my client code so I can determine how to form the api calls in Dev verses in Production environments.
Maybe a webpack devServer proxy would be worth pursuing.
devServer: {
...
proxy: {
'*/controller/*': {
target: 'http://localhost:3002'
}
}
The client would remain blissfully unaware of the differences between development/production.

socket.io.js not loading

I know there are a bunch of questions on this already, but none have answered it for me, and plus mine is slightly different.
I'm starting the socket.io server in node using this:
var io = require('socket.io').listen(8000);
My terminal says everything is ok:
info - socket.io started
Now I am trying to load the .js file in my clientside browser using this url:
http://<hostname>:8000/socket.io/socket.io.js
I dont get a 404, it just hangs forever. I've also used a network utility to ping port 8000, and it seems to be open fine.
I installed node and socket.io just yesterday, so they should be the latest versions. Can anyone shed any light on this? Thanks!
Turns out the reason I could never request the .js file was because my company network blocks all ports except the usual ones (80, 21, etc), so `I would never be able to communicate with port 8000.
Use express.js. Place the socket.io file in public/javascripts folder and add this line to your html
<script src="/javascripts/socket.io.js"></script>
I think this is the best way. When you're writing http://<hostname>:8000/socket.io/socket.io.js
node tries to find a folder named socket.io in your project's public folder. And the file socket.io.js in it.
If you don't want to use express.js you should catch the request and try to load a file if no routes were found for your request (what actually express does) because node doesn't know what to do for requests which don't match any routes in your server.
And I recommend to use the socket.io.min.js file (it's smaller and it's in folder node_modules\socket.io\node_modules\socket.io-client\dist)
You have to start an http/https server to access it via http/https. Simply starting an socket.io server won't do. Do the following:
var http = require('http');
var app = http.createServer(),
io = require('socket.io').listen(app);
app.listen(7000, "0.0.0.0");
Then I can access the file http://localhost:7000/socket.io/socket.io.js
sockets.io uses websocket protocol (ws://). See the wikipedia page.
You need to get at least 3 pieces working together.
Serve some HTML (/index.html will do just fine) so there's a web page. This file should contain the socket.io client <script> tag. For this you need the http server portion of the starter examples. You are missing this and that's why browsing to your server just hangs.
Serve the socket.io client. Socket.io will do this for you automatically when you pass in your http server function to it. You don't need full express as this can be done with just node's http module as per the first example on the socket.io docs.
Some javascript to actually do something with the socket. This can be the content of a <script> tag in index.html or a separate file. If it's a separate file, you need to set up your http server to actually serve it.

Categories