Search code examples
javascriptnode.jssails.jsloopbackjsrestify

Node API Framework


I have a server side API written in Java that provides all the dynamic data for my web application. I am writing a new front end with Ember.js. I want to create a new back end using Javascript that uses the old API for dynamic data. Basically middleware. I have a big JSON file that contains the information for every page (title, description, URL to get the dynamic data from, etc).

website: {
  tab1: {
    title: "Tab 1",
    pages: [
      page1: {
        title: "Page 1",
        description: "Description 1",
        url: "url containing data"
      },
      page2: {
        title: "Page 2",
        description: "Description 2",
        url: "url containing data"
      }
    ]
  },
  tab2: {
    title: "Tab 1",
    pages: [
      page1: {
        title: "Page 1",
        description: "Description 1",
        url: "url containing data"
      },
      page2: {
        title: "Page 2",
        description: "Description 2",
        url: "url containing data"
      }
    ]
  }
}

When I make a request to http://server/ I want to receive ["Tab 1", "Tab 2"].

When I make a request to http://server/tab1 I want to receive ["Page1", "Page 2"].

When I make a request to http://server/tab1/page1 I want to receive {title: "Page 1", description: "Description 1", data: {DATA FROM URL PROCESSED INTO JSON}}.

What server side framework would work best for this? There is no database and no changing of the JSON file. There are update/create requests to the API but the middleware will just change the format of the request and pass it to the old API. I hope to one day expand the middleware API to replace the old one completely so I would like a framework instead of writing it all myself as the framework will be easier later on. I have never used REST API frameworks so any insight is greatly appreciated.


Solution

  • You can easily implement this using Express by defining the routes you need.

    One approach would be to load the JSON file when Express starts up and use the route parameters to dynamically fetch the pieces you want from the JSON. If the JSON is huge and you can't fit in memory then I would use a DB instead. But you said 100's of pages which doesn't seem much

    Then, in your route handlers, you would filter the JSON object based on the route parameters. For example:

    app.get('/:tab/:subtab/:page/:resource/:etc', function(request, response, next) {
     var tab = request.params.tab;
     var page = request.params.page;
    
     // Write a filter() function that extract JSON using tab and page...
     var json = filter(tab, page); 
     return response.send(json);
    });
    

    Here is the guide to Express routing:
    http://expressjs.com/guide/routing.html