Setup a Rails app to avoid having to use CORS - ruby-on-rails

I am running a MAMP server with default settings (port 8888) locally, and I have a Rails app running on its default port (3000).
In production, I will be having a Kentico server running that will need to run GET, POST and PUT requests via ajax to the Rails app.
Right now though, I am just wanting to set it up in a way so that locally I can develop this out without having to implement any form of CORS. Since POST and PUT requests are part of the mix, there's no way I can use JSONP.
Is there any way to do this, and if so will it translate to the same way of doing it once in production?

CORS would be the most standards compliant way of doing cross-domain communications. But other options include:
Set up a proxy server to proxy requests on the server-side to your Rails app
Use an iframe-based solution. This involves setting up a page on your server that makes same-domain XHR requests. You'd then include this page on the client-side using an iframe, and talk to it via inter-frame communication mechanisms such as postMessage.
Here's an overview of the latter technique: http://softwareas.com/cross-domain-communication-with-iframes

Related

How to make requests to a local Rails app from a local AngularJS app running on a different port?

I'm developing a web application with an AngularJS frontend and a Rails backend.
My goal is to keep the two entirely separate, since the Rails app will ultimately be relegated to a simple REST server, and the AngularJS frontend will be just one of a few different clients that use the backend. For that reason, I don't want to integrate the frontend into the Rails asset pipeline, or similar.
The Rails app is running locally on port 3000. The frontend uses Gulp to compile static assets, and BrowserSync, which contains a development server that I'm running on port 3001.
For development purposes, how can I get the AngularJS app to talk to the Rails server, avoiding this browser error (which I'd expected) when making HTTP request via Angular's $http service?
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3001' is therefore not allowed access.
Do I just need to set up CORS in the Rails app? Is there another way - maybe some sort of local hosts file trick (I'm on a Mac, FWIW) or similar? I ask because my coworker is responsible for the Rails app (I have limited experience with Rails) but is out of town for a while; my knowledge is mostly limited to the AngularJS stack that I've set up.
I've searched and read up for about an hour, but haven't yet been able to find anything that I can grok that is applicable to my situation. I might not be aware of the best search terms.
Thanks for any help!
Ah, wonderful. Here are a few solutions:
set up CORS, which shouldn't be too hard. All you have to do is add the headers to pages serving your static content (not the rails REST endpoints), The problem with this is you shouldn't really be doing it unless you have a good reason (so see 3). How are you serving the static content? There should be plugins to do the headers for you if it's a popular tool
serve the angularJS pages from the rails backend for now (using some kind of static route)
if you're thinking about production and want to sort this out for good, use nginx to proxy the two services into one single domain, http://nginx.org/en/docs/beginners_guide.html

Web server for testing POST requests

I am developing part of an application which will POST requests to another server, and examine the request as the web server would see it.
Is there any web server that I can set up (quickly and easily) that will allow me to view the requests to it, and return a sensible code. It doesn't need to process the data in any way. It basically just for development / testing purposes.
You can use REST-client tool to observe the performance.

How to serve a frontend that gets data from a rails API server?

We have decided to go with a rails JSON only API server for our new project since different clients need to access it.
I'm thinking about how the frontend could be served to the browser. As I see it I could
EITHER
have a javascript heavy client built on top of a client side MVC like
Backbone that accesses the API server directly
OR
have the frontend server make some of the API calls and render the html to the client. Some APIs would still be accessed from client JS. A lot of the processing and routing logic would reside in the server.
I'm hesitant to go with the first option due to it being more heavy on the client and less SEO friendly. Also making the client an SPA is not a priority.
I'm looking for advice and comments on the second option. Is this approach recommended ? What would be a good choice for the FE server in that case ? I'm thinking of having a separate rails FE server that talks to the rails API server.

Integrating Guacamole Java Servlet with Rails project

I'm trying to add Guacamole (An html5 vnc client) to an existing rails project but I'm running into some trouble because the Guacamole server is implemented in Java. Based on the overview here http://guac-dev.org/doc/gug/writing-you-own-guacamole-app.html, I need to create 1. a GuacamoleHTTPTunnelServlet (a tunnel between the JavaScript client and the Guacd service) and 2. the javascript client itself. See attached picture for reference. Creating the javascript client seems easy because all the javascript is already given and I would just have to add it to a rails view. The hard part, if possible at all, is integrating the GuacamoleHTTPTunnelServlet java servlet with rails.
Is there any way to have rails serve up the javascript but have the javascript communicate with a different server on the same machine? I'm guessing no because of the same origin policy.
Is there any way to forward the javascript calls from rails server -> java servlet without losing performance? I'm not completely clear on how the javascript client communicates with the server but I think it's passing java objects.
I've never tried anything like this before so please excuse me for any stupidity.
I played around with guacamole and I think your best option is to rewrite guacamole backend (that comunicates with guacd daemon) in rails. Anyway I will try to answer to your questions:
You can proxy ajax requests with rack, ex:
How do I proxy AJAX requests with Rack Middleware?
Another way is to use a reverse proxy (nginx?), ex:
http://yourdomain.com/your/rails/view/url
http://yourdomain.com/guacamole
In this manner the client (browser) will think that your applications are under the same host, avoiding the javascript same origin policy. An iframe will be a great solution.
Javascript communicates with tunnel servlet that proxies requests to guacd daemon (no java objects, just a custom protocol). To speed up performances you can use a reverse proxy (answer 1, ex: nginx) instead of ruby/rack solution.
I hope this can help :)

Data scraping from the web using dart

I am making a web application, in which i need to scrape the web to get some data. I can't see a way to do this without using the dart:io.HttpConnection which is not imporatble for web apps. What should i do, Can i make a server application and then use it with a client version, or something else?
You would need to build this server side since the browser security model does not allow you to connect to other origins than the one that served your application (unless of cause you can use JSONP or CORS to do the scraping but I doubt that). So you need to create a service on your server that uses HttpClient to do the scraping for you and then call this service from your client using XMLHttpRequest

Resources