angular-ified version of the request.js module to avoid CORS?

34 views
Skip to first unread message

Simon Carter

unread,
Apr 1, 2016, 6:37:25 PM4/1/16
to AngularJS
I am looking for an angular-ified version of the request.js module - https://www.npmjs.com/package/request

I have used it successfully from the Windows CLI in a small node.js app to ignore CORS :-)

I cannot use $.ajax, $.post, $resource as they all insist on CORS. 

I have been unsuccessful in incorporating it in an angularjs project. Is there an angular-ified version of this module? Strangely, it appears that request.js is included at least 5 times in the various angular modules I use already, but I have no clue how to access it directly. Any help greatly appreciated!

Also, the jshint linter dies on the request.js code. I could add in all the missing ; and put commas on their preceding lines, but I don't know how to rewrite lines like this:

var http = require('http')
  , https = require('https')
  , url = require('url')
  , util = require('util')
  , stream = require('stream')
  , qs = require('qs')
  , querystring = require('querystring')
  , zlib = require('zlib')
  , helpers = require('./lib/helpers')
  , bl = require('bl')
  , hawk = require('hawk')
...

as it seems require is a node function and is not available to angular.

Ideally I could include it like this:


angular.module('app.data.services', 'request', ['ngResource'])

    .factory('restApi', ['$resource',
    '$q',
    '$ionicLoading',
    'Request',


    function($resource,
        $q,
        $ionicLoading,
        Request
        ) {

...





Please point me in the right direction!

Regards,


Simon

Sander Elias

unread,
Apr 2, 2016, 3:14:01 AM4/2/16
to AngularJS
Hi Simon,

Node has no CORS issue. You can use whatever you want in node to retrieve content, and it will just work.
CORS is a server/browsers interface to protect the server from being used as a free resource.

So yes, requirejs is node-only, and that it has no CORS issue in node. However, if you get it running in the browser(unlikely..) it also will have an CORS restriction on it.

There are 2 ways you can access an CORS protected resource.
  1. ask the server-owner to allow your app. (easiest way)
  2. use a CORS proxy. (simplest form, write an endpoint in your own server that fetches the resource you need)
Hope this helps you a bit,
Regards
Sander

Simon Carter

unread,
Apr 3, 2016, 6:46:57 AM4/3/16
to AngularJS
Thanks Sander, but no.

Note - I'm not talking about requirejs, I'm talking about https://gitter.im/request/request - which is different.

 

https://gitter.im/request/request works in a simple node app from the command line. NO CORS.


But using any angular functions ($.ajax, $resource, $.post etc) in my angular app (ie a different app) does not work due to CORS. Different library, different results.

require.js uses sockets, but angular's functions are higher level and insist on CORS.


- Simon

Simon Carter

unread,
Apr 3, 2016, 6:57:54 AM4/3/16
to ang...@googlegroups.com

Note - I'm not talking about requirejs, I'm talking about https://gitter.im/request/request - which is different.

 

https://gitter.im/request/request works in a simple node app from the command line. NO CORS.

But using any angular functions in my angular app (ie a different app) does not work due to CORS.

 

My simple app that works is

https://github.com/datamystic/share2predictbgl-bridge

 

 

 

Let me know if I can help further.

 

Regards,

Simon Carter,
DataMystic.com  Skype:datamystic  +61.3.9028.8594 (GMT+10 hours) 

PS. Know someone with diabetes? Tell them about PredictBGL Insulin Dose Calculator PREDICTS future blood sugars, avoids hypos and coaches to better doses and better health

TextPipe: PCMAG - "The ultimate text conversion and manipulation tool"

Detach Outlook Attachments and send huge files - DetachPipe.com

Download the latest Windows, Mac and iPhone software - DownloadPipe.com

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/byzzwmcuQTY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Martin Wawrusch

unread,
Apr 3, 2016, 9:50:13 AM4/3/16
to AngularJS, simon....@datamystic.com
Sanders was talking about request. And he is right. There is no way around CORS when using the browser as it is embedded in the network stack. You have the following options, as Sanders said:

* If you control the server add CORS support there
* You can ask whomever controls the server to add CORS support
* You can write a simple proxy that supports CORS and proxies to the server in question.

Sander Elias

unread,
Apr 3, 2016, 10:12:48 AM4/3/16
to AngularJS
Simon, 

While I was talking about requestJS, somehow I typed requireJS. 
To make it short, if you are in a browser, there is NO WAY around CORS.  The only solution to CORS issues is to solve it server side.

Regards
Sander

Simon Carter

unread,
Apr 3, 2016, 10:18:40 AM4/3/16
to Martin Wawrusch, AngularJS

Ok, thanks Martin,

 

Now I need to dig deeper into how my current working Windows CLI code makes the request without proxy and without triggering CORS.


- Simon

Sander Elias

unread,
Apr 3, 2016, 10:50:07 AM4/3/16
to AngularJS, mar...@wawrusch.com, simon....@datamystic.com
Hi Simon,

well, that's quite easily explain. CLI programs using node, don't have CORS. There is NO CORS in the nodejs network stack. 

Here are some resources where you can learn a bit about CORS:

The CORS SpecMore details on how to enable CORS on your server

Hope this clears it up for you a bit,
regards
Sander

Simon Carter

unread,
Apr 4, 2016, 12:11:36 AM4/4/16
to AngularJS, mar...@wawrusch.com, simon....@datamystic.com
Thanks Sander - that has saved me a lot of time! I will now try to implement with a web proxy.

- Simon
Reply all
Reply to author
Forward
0 new messages