Couchbase Light CORS

600 views
Skip to first unread message

stefan...@gmail.com

unread,
Jun 4, 2014, 5:19:53 AM6/4/14
to mobile-c...@googlegroups.com
Hi guys,

I develop a Phonegap application using AngularJS and couchbase light. I'm testing my app in the browser. 
Now I have my device attached and the app running. Then I tunnel the couchbase light port to my local computer using "adb forward tcp:5984 tcp:5984".

This way my javascript REST calls to the database gets forwarded to the database on the device. 

The problem with this: Since CORS is not supported in the couchbase light servlet, I got a "400 Error, saying Access-Control-Allow-Origin is not allowed from ths resource".
The simplest solution would be to build my own version of the "cblite-listener" and add something like "response.addHeader("Access-Control-Allow-Origin", "*")".

Is there any other solution out there?

Cheers guys,
Stefan

Traun Leyden

unread,
Jun 5, 2014, 7:48:40 PM6/5/14
to mobile-c...@googlegroups.com
Not that I know of .. did "response.addHeader("Access-Control-Allow-Origin", "*")" work for you?




--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/9e2f0d76-1a05-4976-99e7-b1f42fb6ae3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

stefan...@gmail.com

unread,
Jun 9, 2014, 7:08:16 AM6/9/14
to mobile-c...@googlegroups.com
Haven't tried it yet, but I will and give you feedback.

stefan...@gmail.com

unread,
Jun 9, 2014, 12:10:22 PM6/9/14
to mobile-c...@googlegroups.com
So I tried out my approach and it works. You have to do two things:

1) add the specific CORS headers to each response
2) handle OPTIONS requests, since these requests are automatically send from most of the browsers as a "preflight" CORS request and couchbase light has no handler for the OPTIONS method.

The class to be modified:

The code:

@Override
public void service(HttpServletRequest request, final HttpServletResponse response)throws ServletException, IOException {

// Handle OPTIONS request in order to respond to CORS
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
response.setHeader("Access-Control-Allow-Headers", "content-type, accept");
if("OPTIONS".equals(request.getMethod())){
  Log.v(Log.TAG_LISTENER, "Handle OPTIONS request in order to respond to CORS");
  response.setStatus(200);
  return;
}
...
}

I also attached the modified library for the Phonegap plugin for Android (couchbase-lite-java-listener-1.0.0-72.jar).

Hope that helps, cheers,
Stefan


couchbase-lite-java-listener-1.0.0-72.jar

J. Chris Anderson

unread,
Jun 9, 2014, 12:13:40 PM6/9/14
to mobile-c...@googlegroups.com
When I've done this in the past I just have Couchbase Lite serve the HTML from an attachment. Like this app does: https://github.com/natevw/CouchTalk-iOS

Juan Hernandez

unread,
Nov 6, 2014, 6:22:38 AM11/6/14
to mobile-c...@googlegroups.com
Hi,

when developing Angular apps I usually serve the application by NGINX server and use a virtual host to serve the application.
When I need to access the backend API or any other services like CouchDB I define a proxy so I don't need to do any CORS requests as it's served by the same host.

This is a sample setup in nginx.conf:

server {
listen 80;
server_name foo.dev;
server_name foo.local;

# Static resources
location / {
root /Users/Juan/Projects/foo/app;
autoindex on;
try_files $uri $uri/index.html $uri/ =404;
}

# Backend services
location /api {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# CouchDB
location /db {
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

My application can request CouchDB using via /db path.
I guess you can do something similar for your Couchbase Lite listening on port 5984.

Hope it helps
Juan

Michael Hines

unread,
Nov 24, 2015, 12:00:00 AM11/24/15
to Couchbase Mobile
Stefan,

Over 1 year later, this code still works. Huge help, Dude. Thanks for the post.

- Michael
Reply all
Reply to author
Forward
0 new messages