0

To begin, this question deals primarily with HTTP requests, BackboneJS, some sort of RESTful API (such as Slim API), and how these things work with each other. Additionally, this question is coming from someone who doesn't have much experience on the server-side, other than just handling basic PHP/MySQL stuff.

I've been looking at Backbone, and I've seen some tutorials regarding the use of RESTful APIs on the back-end (including this one from 9bit).

I also read this answer to a StackOverflow question (Understand BackboneJS REST Calls).

If I open up a JS file, and type type in some code to send a POST request such as this:

(function() {
var http = new XMLHttpRequest();

var value = '{ "prop1": "value 1", "prop2": "value 2" }';

http.open('POST', 'dir', true);
http.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
http.setRequestHeader('Content-Length', value.length);
http.onreadystatechange = function () {
    if (http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(value); 
})();

I see in the console that it sent a POST request looking something like this:

Method: POST
Body: { "prop1": "value 1", "prop2": "value 2" }
Location: http://localhost/~myusername/Todo/dir/

And then, since it's just my local server, the server sends back a response with the basic HTML page that shows the files in that directory, etc.

I tried using a GET request to retrieve a similar JSON object as well, but obviously I couldn't get anything from that location, presumably because the object I was trying to request from some empty folder doesn't even exist anywhere.

My question is, when you use a BackboneJS method such as .save(), from what I understand, it might use, in that instance, a PUT method to send a request with a body of an object, perhaps parsed as a string, to a directory, such as 'article/id', with 'id' possibly being something like '43' (potentially the corresponding id of whatever model's properties you sent). So...

1) What does an API, such as Slim do with that request?
2) Where is it saving those object properties to (a MySQL database)?
3) Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database? (I'm wondering why the location is simply a 'folder', such as '/article'. To put it in another context, whenever you type in a website like 'http://www.mywebsite.com', the server will automatically look for an 'index' page in that directory, such as 'index.html', and automatically open that file as the default file of that directory. Is that same type of thing happening in the context of using a location '/somefoldername' as the location of the HTTP request)?

Basically, it just looks strange to me that you would send an HTTP request to just some folder, and not a specific PHP file (for example) that would handle the request and communicate with a database. Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason?

Thank you very much.

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • It sounds like you are getting a directory listing instead of code actually being called, is that right? did you configure your server to handle PHP files? – Noam Rathaus Dec 29 '13 at 10:30
  • Yes, I have PHP installed, etc. My AMP stack works fine, if for example I send a POST/GET request to a file (say, 'add_to_db.php'), which communicates with my MySQL database. No issues in that regard. – Josh Beam Dec 29 '13 at 10:32
  • And yes, I expect to get a directory listing if I'm sending a request to an empty directory. My question is simply, why when using Backbone and a RESTful API would you send a request to _just_ a directory and not a script file (like a PHP file)? Is there something I'm missing here? – Josh Beam Dec 29 '13 at 10:33
  • It doesn't make sense to me to request a `dir` rather than `file`, where did you see (documentation) that you needed to do that? – Noam Rathaus Dec 29 '13 at 10:34
  • In the links listed in the first portion of my question. In addition, there are numerous tutorials all over the web if you do a Google search regarding BackboneJS and how it works alongside a RESTful API on the server. – Josh Beam Dec 29 '13 at 10:35
  • The `/` is being called as a way of listing, for example /tasks/ will return all `tasks` waiting for process, it is not meant to be an actual directory listing, are you seeing the php files? or the stored item in a form of "directories"? – Noam Rathaus Dec 29 '13 at 11:04
  • I don't understand that. How would the location mywebsite.com/tasks/ list, for example, tasks waiting for process? How is /tasks/ not a directory? – Josh Beam Dec 29 '13 at 11:07
  • It is a `directory` when viewed via the web browser, but not as in a directory, physically as far as I can read from the documentation of backbone.js – Noam Rathaus Dec 29 '13 at 11:08
  • Also, if you check out [this answer](http://stackoverflow.com/a/4701836/2714730), the same thing is going on... To me, it looks like they're saving an object as /user/12345. – Josh Beam Dec 29 '13 at 11:09
  • A nice example is this http://localtodos.com/ , look at what it does – Noam Rathaus Dec 29 '13 at 11:09

1 Answers1

2

Since you asked your questions in the context of Slim, I'm going to answer them that way, although much of the information will generally apply to other web applications/frameworks.

What does an API, such as Slim do with that request?

Not to be to cheeky, but it does whatever you (or the API developer) wants it to do (more on that shortly).

Where is it saving those object properties to (a MySQL database)?

In general, those object properties are being used to create a resource (POST) or to updated a resource (PUT), and most likely the resource is being persisted in some sort of storage, be it an RDMS or a NoSQL solution.

Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database?

Here's where things get interesting, IMHO. Considering the route article/id, here's what happens in a Slim application:

  1. A request is received at example.com/article/22
  2. The request is routed to a front controller script which, based on the request URI and HTTP method, makes a decision about what to do with the request.
  3. If a PUT route for article exists, then the application code perhaps would take the request body and update the resource identified by the provided id.

With that in mind, in a Slim application, likely the only web accessible file is index.php. What appear to be directories are merely routes defined in the application's index.php that Slim used to make decisions about how to handle requests. With that in mind . . .

Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason?

In the context of a Slim application, no, /article wouldn't exist as a directory, but rather a route.

Perhaps this won't help much, but this is what a portion of that index.php routing file might look like on the Slim side:

$app->post('/article', function () {
    // Get data from post
    // Create resource
});

$app->get('/article/:id', function ($id) {
    // Return an article resource identified by $id
});

$app->put('/article/:id', function ($id) {
    // Use $id to retrieve resource from storage
    // Update resource with request data
});
Jeremy Kendall
  • 2,869
  • 17
  • 17
  • Okay, so I click on a link in my main page (index.php). The link sends a POST request with a URI of /article/43. The script inside index.php (utilizing Slim's API) tells the browser that if it sees the URI /article/43, that it should execute a callback function, perhaps with parts of the requested resource being parameters for the function. The callback function could, in theory, return a JSON object from a NoSQL database, etc. Is that somewhat on the right track? – Josh Beam Dec 29 '13 at 11:21
  • In this circumstance, would this be referred to as routing? – Josh Beam Dec 29 '13 at 11:22
  • 1
    Bingo. The "routing" bit is how the API can respond to requests made to '/article' without an '/article/index.php' being present in the app. It might help to see a live example. This index.php (https://github.com/jeremykendall/flaming-archer/blob/develop/public/index.php) is the front controller for this application (http://365.jeremykendall.net/). The POST and DELETE routes for the photo endpoint might be especially relevant. – Jeremy Kendall Dec 29 '13 at 11:30
  • @JoshBeam Also see this Q and A: http://stackoverflow.com/questions/9816274/ways-to-save-backbone-js-model-data. Lots of good, relevant Backbone.js info and the answer uses Slim. – Jeremy Kendall Dec 29 '13 at 11:40