Skip to content

Configure remoting options via app.set and config.json #608

Closed

Description

@bajtos

#433 adds a remoting option normalizeHttpPath. At the moment, the only way how to set this options is to modify server/server.js and change the line calling loopback.rest().

We should modify loopback.rest() to read default options via app.get(). Since app.get() options are already populated from config.json by loopback-boot, this change will allow developers to keep the configuration in json files.

Example configuration in config.json:

{
  "port": 3000,
  "restApiRoot": "/api",
  "remoting": {
    "normalizeHttpPath": true
  }
}

Implementation in app.handler():

options = extend(this.get('remoting') || {}, options);
var remotes = this.remotes();
var handler = this._handlers[type] = remotes.handler(type, options);

/cc @fabien @ritch

Activity

  1. ritch commented on Oct 2, 2014

    @ritch
    Member

    👍

  2. fabien commented on Jul 8, 2015

    @fabien
    Contributor

    @bajtos @raymondfeng unfortunately, normalizeHttpPath: true from server/config.json doesn't seem to work as expected. The option is never made available to https://github.com/strongloop/loopback/blob/master/lib/model.js#L120 which means the only way to currently do this, is using Model.settings.remoting.normalizeHttpPath = true, which defeats the purpose. It's usually something you'd switch on for your whole app.

  3. bajtos commented on Jul 9, 2015

    @bajtos
    MemberAuthor

    If I am reading #164 correctly, then you need to set the flag inside rest section:

    {
      "remoting": {
          "rest": {"normalizeHttpPath": true}
      }
    }

    I think it's not necessary to pass the option to https://github.com/strongloop/loopback/blob/master/lib/model.js#L120, because the app-wide setting is IMO used as the default value - see your PR strongloop/strong-remoting#90.

  4. fabien commented on Jul 9, 2015

    @fabien
    Contributor

    @bajtos it is already in the rest section (by default, in a new LB project), yet I don't see how the app-wide setting is used. I tried, but it's never passed to that particular point in the code, and unfortunately I don't see a way to get the app-wide setting there (app is not available in model.js I think).

  5. bajtos commented on Jul 9, 2015

    @bajtos
    MemberAuthor

    I see, I guess it is a bug then. I am afraid I don't have enough time to investigate this now myself, sorry for that.

    I quickly looked at https://github.com/fabien/strong-remoting/blob/7a46b050b8156d1d6ae10e1283f1444da0c52b4d/lib/remote-objects.js#L135-L145, it seems that global options are applied only to classes gathered via exports, not to classes already registered via _classes. Perhaps that's the root of the problem?

  6. kevinji commented on Jul 15, 2015

    @kevinji

    @fabien Since the option is currently broken, is there a workaround that can force the enabling of normalizeHttpPath? Your suggestion of Model.settings.remoting.normalizeHttpPath = true doesn't seem to quite work.

  7. fabien commented on Jul 16, 2015

    @fabien
    Contributor

    @mc10 it should work, but I guess it depends on where you declare this setting.

    Have you tried adding this in your model.json?

    properties: { ... },
    remoting: {
      normalizeHttpPath: true
    }
    
  8. fabien commented on Jul 16, 2015

    @fabien
    Contributor

    @bajtos yes, this is the issue indeed I think.

  9. kevinji commented on Jul 16, 2015

    @kevinji

    Do I need to add this to each individual model's .json files? That seems quite unwieldy, and you can't change the built-in models either.

  10. fabien commented on Jul 16, 2015

    @fabien
    Contributor

    @mc10 I'm afraid this is the only option right now, we need to look in to the issue. You might be able to iterate over all models in a boot script, and set normalizeHttpPath on each model. For this to work, the bootscript should run before the point where the api is initialized on boot.

  11. kevinji commented on Jul 16, 2015

    @kevinji

    I'm not sure how I'd go about iterating through the models individually--would you mind explaining? As a stopgap measure I've used a remoting option in each individual model's JSON file.

  12. 19 remaining items

  13. crandmck commented on Jul 24, 2017

    @crandmck
    Contributor

    @lehni Will the above PR require any changes to docs (once it's merged)?
    If so, could you summarize please?

  14. lehni commented on Jul 27, 2017

    @lehni
    Contributor

    @crandmck I will look into documentation requirements once the PR landed. I believe that what I found online is actually vague enough that it is not wrong for the new behavior.

    Bu I just realized that while normalizeHttpPath is mentioned in the documentation of config.json for LB 2, it is gone from LB 3's docs:

    https://loopback.io/doc/en/lb2/config.json.html
    https://loopback.io/doc/en/lb3/config.json.html

    Perhaps it's then simply a question of bringing it back?

  15. crandmck commented on Jul 27, 2017

    @crandmck
    Contributor

    I'm sure it was removed for a reason, but if this PR resurrects it, then it's easy enough to reinstate it in the v3 docs. Please advise!

  16. bajtos commented on Jul 28, 2017

    @bajtos
    MemberAuthor

    IIRC, rest. normalizeHttpPath path does not work and the feature should be configured in server/middleware.json instead. I am not entirely sure about this, it would be best to add a unit-test to loopback project to verify which approach actually works: remoting options in config.json or loopback#rest parameters in server/middleware.json or perhaps both.

  17. lehni commented on Jul 28, 2017

    @lehni
    Contributor

    rest.normalizeHttpPath works for me with the above PR, no need for middleware. I am happy to look into unit tests for the various scenarios. Into which module would they go? strong-remoting also?

  18. bajtos commented on Jul 28, 2017

    @bajtos
    MemberAuthor

    I am happy to look into unit tests for the various scenarios. Into which module would they go? strong-remoting also?

    That's great to hear! These tests should go to strongloop/loopback, to verify our assumptions about how strong-remoting works inside LoopBack apps. You will need to install your development version of strong-remoting into your loopback project, I personally use npm link.

    We already have a test for model-level normalization here, so just add a new test for application-level normalization.

  19. lehni commented on Jul 28, 2017

    @lehni
    Contributor

    I think I need some help to get this particular test running. I thought I tracked it down to:

    grunt karma:unit-once

    ...or this, if I wanted to run the tests repeatedly while editing them:

    grunt karma:unit

    To test things, I then changed this line:

     request(app).get('/user-accounts').expect(200, done);

    ...to:

     request(app).get('/UserAccounts').expect(200, done);

    ...hoping, it would trigger an error when running the tests again, but it did not.

    What am I doing wrong?

  20. lehni commented on Jul 28, 2017

    @lehni
    Contributor

    Ok, my bad... Turns out it's on the mocha side of things:

    grunt mochaTest:unit
  21. lehni commented on Jul 29, 2017

    @lehni
    Contributor

    @bajtos I've created a PR for the tests now: #3527

  22. bajtos commented on Aug 3, 2017

    @bajtos
    MemberAuthor

    Closing as done by strongloop/strong-remoting#410 and #3527. Thank you @lehni for this valuable contribution! 👏

  23. crandmck commented on Aug 3, 2017

    @crandmck
    Contributor

    So, should I put rest.normalizeHttpPath back into the v3 docs?

  24. bajtos commented on Aug 4, 2017

    @bajtos
    MemberAuthor

    So, should I put rest.normalizeHttpPath back into the v3 docs?

    @crandmck yes please!

  25. lehni commented on Aug 7, 2017

    @lehni
    Contributor

    @bajtos great news, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions