locked
[Node.js][Mobile Apps] Mobile apps node.js backend custom API with multiple routes RRS feed

  • Question

  • I have been trying with the new mobile apps to do a custom API with multiple routes, but I can't get it to work.

    The example here seems not to work anymore

    https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#api-routes

    I want to do

    GET: api/user/:id - should return only details for one user

    GET: api/user/allUsers - should return a list of all users

    How do I do this in mobile apps?



    Wednesday, April 6, 2016 9:06 PM

Answers

  • Hey Marten,

    Mobile Apps doesn't support the same style of custom API routing that Mobile Services did. The main reason for this is that custom APIs are really just plain express middleware, it's fairly trivial to mount it directly, something like:

    app.use('/api/user/:id', require('./api/byUserId'))
    app.use('/api/user/allUsers', require('./api/allUsers'))

    or something similar that suits your needs.

    Having said this, we have a number of issues open that are related to this:

    https://github.com/Azure/azure-mobile-apps-node/issues/350
    https://github.com/Azure/azure-mobile-apps-node/issues/102
    https://github.com/Azure/azure-mobile-apps-node/issues/96

    We may implement this in the near future depending on demand.

    Hope this helps!

    Dale

    Wednesday, April 6, 2016 10:25 PM
  • Hi Martin,

    You can find the initial version of the sample at https://github.com/danderson00/azure-mobile-apps-node/tree/routing/samples/custom-api-routing. It hasn't been merged into the main repository and may undergo some changes, but should give you the idea.

    We are looking at introducing DocumentDB support in the near future. Keep an eye out! Please let us know if there are any other features you think are missing.

    You can still debug locally when using push notifications, though the actual push still happens through your Notification Hub, so you still need an Internet connection and a configured Notification Hub.

    Cheers!

    Dale

    Monday, April 11, 2016 9:14 PM

All replies

  • This is a wrong use of REST.

    • GET api/user/:id should return only details for one user
    • GET api/user should return all users

    Is there any particular reason you are not using a table? 

    Wednesday, April 6, 2016 9:36 PM
  • Hey Marten,

    Mobile Apps doesn't support the same style of custom API routing that Mobile Services did. The main reason for this is that custom APIs are really just plain express middleware, it's fairly trivial to mount it directly, something like:

    app.use('/api/user/:id', require('./api/byUserId'))
    app.use('/api/user/allUsers', require('./api/allUsers'))

    or something similar that suits your needs.

    Having said this, we have a number of issues open that are related to this:

    https://github.com/Azure/azure-mobile-apps-node/issues/350
    https://github.com/Azure/azure-mobile-apps-node/issues/102
    https://github.com/Azure/azure-mobile-apps-node/issues/96

    We may implement this in the near future depending on demand.

    Hope this helps!

    Dale

    Wednesday, April 6, 2016 10:25 PM
  • I'm using a table but doing all operations (queries etc.) in the custom API. I don't want the client to perform Azure specific calls. We are trying to make a transparent API layer on the client, where we can change the backend to another provider in the future if we want. It gives much better flexibility.

    I did think of just doing all table operations directly through tables/<table_name>, but that would also require too many round-trips and traffic in general. By implementing certain operations in the custom API, we have full control and client doesn't need to care which provider we are using to do what it needs to do.

    Thursday, April 7, 2016 8:42 PM
  • Wooh, I thought mobile apps should be as equal good as the old mobile services, but it sounds like quite some features were not migrated over. What a shame.

    I didn't fully understand your hack above. Would it today be somehow possible to add inside the custom API definitions multiple routes? I'm not at all experienced with node.js or express for that matter so any help how to throw the correct objects at it is highly appreciated. A full example based on custom API will help me on the way.

    I would for sure request to give priority to this feature and related issues as you mention above.

    Martin

    Thursday, April 7, 2016 9:02 PM
  • Martin,

    Are there any features that you particularly miss aside from custom API routing? We have tried to maintain parity with Mobile Services as much as possible.

    We'll add a sample that covers your scenario in the next couple of days.

    Dale

    Friday, April 8, 2016 10:53 PM
  • Hi Dale

    Would be a great help with those custom API routing examples. Usually the most simple available examples are never enough to solve more complicated things and finding the info how to do it is not easy - if possible at all. Let me know once you have those examples ;-)

    It might be too early for me to say if other features are missing. I'm new to node.js and JavaScript so still learning how to do things (coming from embedded software world JavaScript is pretty different). As I understand it, Mobile Apps tries to solve the puzzle with all needed services (or at least gives options to plug-in more stuff) that one would need to do a modern app/server solution. To me I think you do have all the pieces to put together something.

    Only thing I was disappointed over end of last year, is that we wanted to use NoSQL (documentDB) as data store but there is no integration with your existing services like Mobile apps. We wasted 2 months before I figured that out. It is just a database and one need to do quite some work to make the integration. At least for me not knowing anything about backend and just needs to get something working fast with our startup company it is mission impossible.

    Next part I will look deeper at is push notification, but I think that is one topic that is covered with OK examples as far as I have seen. It would have been perfect to be able to develop/debug locally and use push also but I think it has to be running on Azure to be working unfortunately.

    Martin

    Saturday, April 9, 2016 8:15 PM
  • By the way. Regarding the sample. Remember to include authentication/anonymous options, just like custom API can do it today.

    The example under e2etest/api/movieFinder.js is cheating as it is not using custom API to make multiple routes.

    Monday, April 11, 2016 4:31 PM
  • Hi Martin,

    You can find the initial version of the sample at https://github.com/danderson00/azure-mobile-apps-node/tree/routing/samples/custom-api-routing. It hasn't been merged into the main repository and may undergo some changes, but should give you the idea.

    We are looking at introducing DocumentDB support in the near future. Keep an eye out! Please let us know if there are any other features you think are missing.

    You can still debug locally when using push notifications, though the actual push still happens through your Notification Hub, so you still need an Internet connection and a configured Notification Hub.

    Cheers!

    Dale

    Monday, April 11, 2016 9:14 PM
  • Looks like some of the right stuff you have there. I don't care if the APIs are not visible in the portal, as I'm anyway using Visual studio code locally on my MAC for all changes and then do git push. Thanks for the fast examples.

    As a 2nd request. It would be nice with some node.js azure mobile apps documentation on how Azure is integrated to Express framework. So for novice people like me, I would also understand what middleware you have added, in which order they are expected and so on. It would give a much better picture of the server side of things. I guess everything requires some pre-knowledge on the SW one is working with ;-)

    Let me know when you have a final package in the repo.

    Tuesday, April 12, 2016 8:47 PM
  • Hi Martin,

    2.1.0 is released and includes sqlite support! Make sure to install the sqlite3 module locally if you want to use it. 

    Cheers!

    Tuesday, April 19, 2016 6:02 PM
  • Hi Dale

    Your example works just nicely. However, there is a problem I noticed while testing locally.

    When regular APIs are added using the normal method, accessing an API without x-zumo-auth set and probably the x-ms-client-principal-id set, the auth middleware usually outputs this back:
    You must be logged in to use this application

    With this approach, if x-zumo-auth is not present in the request I get this error instead of the "you must be looged in..." error.

    TypeError: Cannot read property &#39;user&#39; of undefined
    node_modules/azure-mobile-apps/src/express/middleware/authorize.js:5:24)

    The req.azureMobile.user (user part) is never made.

    Can you take a look?

    Saturday, April 23, 2016 8:18 PM
  • The authenticate middleware currently only retrieves JWT tokens from the x-zumo-auth header.  Are you seeing them in other headers? I'll check with the auth team if it can be presented in other headers. Will get back to you on Monday!
    Saturday, April 23, 2016 8:54 PM
  • Hi

    After debugging a bit I noticed in your example that the customAPI router needs registration after the mobile app is registered. When I changed it it worked just like before. No problems. If auth-header is not present an error is send back to client, which is OK.

    Why is registration needed after registration?

    br

    Martin

    Sunday, April 24, 2016 5:14 PM
  • Because the authenticate middleware needs to have executed (as part of the azure-mobile-apps stack) for the req.azureMobile.user object to be populated.
    Sunday, April 24, 2016 7:03 PM
  • Ahh so it is the registration order the middleware is executed? Ok, that explains it.
    Sunday, April 24, 2016 7:31 PM