regarding foodme project in github

Problem

hello i have a question regarding the foodme express example over github:

code:

var express = require('express');
var fs = require('fs');
var open = require('open');

var RestaurantRecord = require('./model').Restaurant;
var MemoryStorage = require('./storage').Memory;

var API_URL = '/api/restaurant';
var API_URL_ID = API_URL + '/:id';
var API_URL_ORDER = '/api/order';

var removeMenuItems = function(restaurant) {
  var clone = {};

  Object.getOwnPropertyNames(restaurant).forEach(function(key) {
    if (key !== 'menuItems') {
      clone[key] = restaurant[key];
    }
  });

  return clone;
};


exports.start = function(PORT, STATIC_DIR, DATA_FILE, TEST_DIR) {
  var app = express();
  var storage = new MemoryStorage();

  // log requests
  app.use(express.logger('dev'));

  // serve static files for demo client
  app.use(express.static(STATIC_DIR));

  // parse body into req.body
  app.use(express.bodyParser());


  // API
  app.get(API_URL, function(req, res, next) {
    res.send(200, storage.getAll().map(removeMenuItems));
  });

i don't understand where is the api folder. it doesn't exist and i don't understand how information is going in and out from there. i can't find it.

can someone please explain this to me?

another question: there is a resource for the restaurant

foodMeApp.factory('Restaurant', function($resource) {
  return $resource('/api/restaurant/:id', {id: '@id'});

});

and in the restaurant controller there is a query:

  var allRestaurants = Restaurant.query(filterAndSortRestaurants);

and the following lines:

  $scope.$watch('filter', filterAndSortRestaurants, true);

  function filterAndSortRestaurants() {
    $scope.restaurants = [];

    // filter
    angular.forEach(allRestaurants, function(item, key) {
      if (filter.price && filter.price !== item.price) {
        return;
      }

      if (filter.rating && filter.rating !== item.rating) {
        return;
      }

      if (filter.cuisine.length && filter.cuisine.indexOf(item.cuisine) === -1) {
        return;
      }

      $scope.restaurants.push(item);
    });


    // sort
    $scope.restaurants.sort(function(a, b) {
      if (a[filter.sortBy] > b[filter.sortBy]) {
        return filter.sortAsc ? 1 : -1;
      }

      if (a[filter.sortBy] < b[filter.sortBy]) {
        return filter.sortAsc ? -1 : 1;
      }

      return 0;
    });
  };

the things that isn't clear to me is: how is that we are giving the query just a function without even activating it. as i understand we should have passed the query somthing like:

{id: $routeParams.restaurantId}

but we only passed a reference to a function. that doesn't make any sense.

could someone elaborate on this?

thanks again.

Problem courtesy of: lobengula3rd

Solution

var API_URL = '/api/restaurant';
var API_URL_ID = API_URL + '/:id';
var API_URL_ORDER = '/api/order';

These lines are just defining string constants that are plugged into Express further down. They're not a folder.

app.get(API_URL, function(req, res, next) {
  res.send(200, storage.getAll().map(removeMenuItems));
});

So this function call to app.get(API_URL... is telling Express "Look out for GET requests that are pointed at the URL (your app's domain)/api/restaurant, and execute this function to handle such a request."

Solution courtesy of: Plato

Discussion

"api" is not a folder.

Every requests will pass through the app.get method.

This method will respond to the routes /api/restaurant as defined in the API_URL variable.

Discussion courtesy of: Aurélien Thieriot

This recipe can be found in it's original form on Stack Over Flow.