loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Setting cors:true in config.json throws exception

Open barocsi opened this issue 11 years ago • 18 comments

\node_modules\cors\lib\index.js:188 o.origin = defaults.origin; ^ TypeError: Cannot assign to read only property 'origin' of true at middlewareWrapper (\node_modules\cors\lib\index.js:188:16) at setupCors (\node_modules\loopback-explorer\lib\swagger.js:145:18) [..]

"loopback-boot": "^2.6.5",
"loopback-component-passport": "^1.5.0",
"loopback-connector-mongodb": "1.13.0",
"loopback-datasource-juggler": "^2.41.1",

barocsi avatar Nov 11 '15 21:11 barocsi

so what led to that error? can you provide the steps in order to reproduce the error? Any repo to be used as well to reproduce the error? thanks.

loay avatar Nov 11 '15 21:11 loay

Simply node . started the app and it threw error right away. Only this parameter was changed in the config file. Here is the full stack trace TypeError: Cannot assign to read only property 'origin' of true at middlewareWrapper (\test\node_modules\cors\lib\index.js:188:16) at setupCors (\test\node_modules\loopback-explorer\lib\swagger.js:145:18) at Swagger (\test\node_modules\loopback-explorer\lib\swagger.js:55:3) at explorer (\test\node_modules\loopback-explorer\index.js:31:3) at Object.mountLoopBackExplorer as func at \test\node_modules\loopback-boot\lib\executor.js:287:9 at iterate (\test\node_modules\loopback-boot\node_modules\async\lib\async.js:146:13) at \test\node_modules\loopback-boot\node_modules\async\lib\async.js:157:25 at \test\node_modules\loopback-boot\lib\executor.js:289:7 at iterate (\test\node_modules\loopback-boot\node_modules\async\lib\async.js:146:13)

barocsi avatar Nov 11 '15 22:11 barocsi

Additionally for which I am confused in the middleware.json cors is enabled "cors": { "params": { "origin": true, "credentials": true, "maxAge": 86400 }

barocsi avatar Nov 11 '15 22:11 barocsi

i used a sample project and I could reproduce the same error when I do node .

for lines 188 and 191 if you use

o.origin == defaults.origin;

instead of

o.origin = defaults.origin;

and in line 194, if you use

o.preflightContinue == defaults.preflightContinue;

instead of

o.preflightContinue = defaults.preflightContinue;

That should fix the issue in your project and run the node server fine. If that doesn't work, can you share your project repo? Thanks.

loay avatar Nov 11 '15 22:11 loay

@barocsi Still having an issue with that? Thanks

loay avatar Nov 16 '15 16:11 loay

Please let me know if the problem is still persisting .. we will re-open the ticket. closes #1810

loay avatar Nov 27 '15 01:11 loay

I am having this issue as well and editing the package directly doesn't feel like a permanent solution... What do you need to help debugging ?

kartsims avatar Mar 15 '16 12:03 kartsims

I am having the same issue. I can't set cors: true in the config.json

.../strongloop-app/node_modules/cors/lib/index.js:188
      o.origin = defaults.origin;
               ^

TypeError: Cannot assign to read only property 'origin' of true

moklick avatar Mar 19 '16 11:03 moklick

@kartsims @moklick

  • but did the solution in this comment: https://github.com/strongloop/loopback/issues/1810#issuecomment-155933790 work for you guys as a temp solution? It did work for me.
  • also, can you please share a repo of your code, so I can reproduce. I will try to push for a permanent solution after I look at your cases.

loay avatar Mar 22 '16 19:03 loay

Hey @loay,

sorry the fix doesn't work for me.. that is nothing I can do in a production app. You can easily reproduce it by creating a new project:

slc loopback
... create empty-server
... set cors: true in config.json
node . 

moklick avatar Mar 23 '16 00:03 moklick

Same here, that is not an acceptable solution to the issue... Doesn't look "fixed" if it needs such a hack.

I did as @moklick said and did get the error :

/Users/simon/dev/loopback/loopback-issue-1810/node_modules/cors/lib/index.js:188
      o.origin = defaults.origin;
               ^

TypeError: Cannot assign to read only property 'origin' of true
    at middlewareWrapper (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/cors/lib/index.js:188:16)
    at setupCors (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:123:18)
    at mountSwagger (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:111:3)
    at routes (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:47:3)
    at explorer (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-component-explorer/index.js:26:46)
    at /Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:398:5
    at Array.forEach (native)
    at setupComponents (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:394:27)
    at execute (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/lib/executor.js:35:3)
    at bootLoopBackApp (/Users/simon/dev/loopback/loopback-issue-1810/node_modules/loopback-boot/index.js:140:3)

I uploaded the code I got, just in case, here : https://github.com/kartsims/loopback-issue-1810

Also I am running the code on OSX with the following versions

MacBook-Air:~ simon$ node -v
v5.4.1
MacBook-Air:~ simon$ slc -v
strongloop v6.0.0 (node v5.4.1)
├── [email protected] (fb756f2)
├── [email protected] (47dd24a)
├── [email protected] (be6180a)
├── [email protected] (62e539b)
├── [email protected] (4197516)
├── [email protected] (f46e58f)
├── [email protected] (1327018)
├─┬ [email protected] (1e39220)
│ └── [email protected] (4ea7ee9)
├── [email protected] (6440776)
├── [email protected]
└── [email protected]

MacBook-Air:~ simon$ npm -v
3.5.3

kartsims avatar Mar 23 '16 07:03 kartsims

Thanks. Having a look. Re-opened issue.

loay avatar Mar 23 '16 13:03 loay

in config.json, instead of setting cors to true, you can set ; "cors": { "origin": true }, which will make the app run fine. you can still set the value of methods and preflightContinue but it is not necessary in config.json. can you guys try it in your app? Thanks.

loay avatar Mar 23 '16 15:03 loay

Works for me :sparkles:

After looking back into the documentation I didn't find where I got the idea that setting it to true would do the trick but it has to be in there, somewhere, I don't think I made this up! If I ever find it again, I'll let you know.

Thanks for the reply @loay

kartsims avatar Mar 23 '16 15:03 kartsims

Glad I can help :)

loay avatar Mar 23 '16 15:03 loay

Now this is a good fix. Thanks a lot @loay

moklick avatar Mar 23 '16 16:03 moklick

I had same error but that was because my jenkin server was already running on 8080 and My node server also trying to create server using 8080 port please check could be same for you too.

iamprohaktiv avatar Jul 04 '16 07:07 iamprohaktiv

this changed :(

  throw new Error(g.f(
    ^

Error: The REST adapter no longer comes with a built-in CORS middleware, the config option "remoting.cors" is no longer available.See https://docs.strongloop.com/display/public/LB/Security+considerations for more details.

heshamelmasry77 avatar Sep 16 '18 15:09 heshamelmasry77