Part 1 - Seriously, What The Hell Is REST?
Part 1 - Seriously, What The Hell Is REST?
js
web app, using the Express framework, that reads from and writes to a MongoDB database.
That's a great start, and if you're unfamiliar with those technologies, now would be an excellent
time to go through the tutorial, because we're about to delve deeper. You're going to need to
know how to get a webserver running with Express, and how to use app.get and app.post to
communicate with both the server and the database. It's all covered in that original tutorial, and if
you're a developer who's familiar with JavaScript, it's not hard. Go check it out!
Back? or rolling your eyes and going, dude, I already know that stuff? Either way, great! Let's
add some new tools to our toolbox, and create a simple little app that works without a single page
refresh. Here are the goals:
Basically, in the first tutorial, we built a simple front-end app atop the Router/View back-end. In
this tutorial, we're going to eliminate the need for page refreshing or visiting separate URIs entirely.
It's all going to work out fine. But before we start building, let's get some REST
JavaScript variables, data- attributes, and so on), and then HTTP GET a newPage service using the
nextPage parameter from the markup to request a specific page.
I've put together a quick JSFiddle illustrating what I'm talking about. Take a look and note that
we're never storing any page data on the server side. We merely take the current page value from
the DOM, and then when we get our new page, we update the DOM. That's simple, stateless
programming.
Expose directory structure-like URIs.
This one's easy. Instead of:
http://app.com/getfile.php?type=video&game=skyrim&pid=68
You want:
http://app.com/files/video/skyrim/68
Transfer XML, JavaScript Object Notation (JSON), or both.
This one's easy too! Just make sure that your back-end is sending XML or JSON (I prefer JSON,
especially in all-JavaScript setups like the one discussed in this tutorial). You can easily manipulate
this data in your presentation layer without having to hit your servers, unless you need new data.
OK so do we get the basics of REST? It's pretty straightforward, really. You've probably already
worked within systems that use it.
Part 2 - Setup
Now that we have an idea of what REST is all about, let's put it to work for us by building a
stupidly simple single-page web app that's completely valueless. Kind of like half the startups in the
Valley, amirite?! Is this thing on?
Anyway, no, we're not building a to-do list, though that's become the Hello World of web apps.
We're going to build a simple collection of usernames and emails, much like we did in our previous
tutorial. However, a few things need to be set up, and also we're going to tear down one thing:
Monk. We're switching over to Mongoskin for our MongoDB management. Why? Well, for one
thing, nothing significant's been committed to the Monk Github page in about a year, and that
makes me nervous. For another, Mongoskin is lightweight and doesn't require you to define
schemas the way Mongoose does, but it's a little more full-featured than Monk. The syntax is almost
identical.
So, let's get started. Make sure you've got the latest stable version of Node installed on your
machine, then fire up a console window and navigate to wherever you're storing these web projects.
For the purposes of this tutorial, that's C:\node\. If you're placing your work elsewhere (for example
/home or /Users), adjust accordingly.
The first thing we'll want to do is update Express and the Express scaffolding generator globally,
like this: