RESTful Routes

What are REST, Routes, CRUD?

Representation State Transfer or REST is a pattern/convention for defining our routes. Routes are the code that listens and receives requests then figures out what to send back. CRUD stands for Create, Read, Update, and Delete. These are the 4 basic functions we have when building an API.

So why do we want any of this? Mostly it’s just about keeping things simple and repeatable when building apps. By following a convention we don’t have to reinvent the wheel every time we make an app and other developers will quickly figure out what’s going on.

The Magnificent 7

REST is made up of 7 standard routes:

NameURLHTTP VerbDescription
Index/teamsGETDisplays a list of all teams
New/teams/newGETDisplays a new team form
Create/teamsPOSTAdds new team to the database then redirects
Show/teams/:idGETShows a particular team
Edit/teams/edit/:idGETShows an edit form for a particular team
Update/teams/:idPUTUpdates a particular team in the database then redirects
Destroy/teams/:idDELETERemoves a particular team from the database then redirects

Installing MongoDB on macOS

The course I’m doing uses Cloud 9 (C9) as cloud-based IDE. I’ve basically ignored that side of things and done everything locally on my Mac as that feels more real world to me. Turns out this was a good decision as C9 has recently announced it’s shutting down. Anyway, at times this has made things more difficult though. A good example is when I need to follow instructions to get a particular part of the development environment setup. Because all the instructions are specific to C9 it means I more or less have to figure out how to do it myself on macOS.

This morning I had an example of this exact thing when I needed to install MongoDB. I’m finally at the point that I’m starting to work with a database (hallelujah!) and I needed to get things going on my Mac. Thankfully there’s Google and I quickly found this treehouse article that gave me some clear steps to follow.

Of course it’s never quite that easy. Different machines are set up in different ways, so a guide like this doesn’t always allow for edge cases. So here’s what worked for me:

  • Open terminal and use the command brew update to update Homebrew itself to the latest version
  • Now install MongoDB using brew install mongodb. This will install the latest version
  • Next we need to create a folder to house our MongoDB data files. Use the command sudo mkdir -p /data/db. The instructions I first followed didn’t include sudo and so didn’t work for me. Your milage may vary
  • We should now have a place for our data but we need to set the folder permissions using sudo chown -R `id -un` /data/db

That’s basically it, you should now have a functional install of MongoDB. Open up two terminal windows in the first start the server with the mongod command. In the second terminal run mongo to launch the mongo shell for accessing data.

YelpCamp

I’ve now reached a turning point in my web dev course. This morning I started writing code on my first meaty project, YelpCamp.

As the name implies the YelpCamp project is Yelp but for campsites. The idea isn’t to release this as a service that people will actually use. The intent is to combine what I’ve learnt from the past few months and use these skills to make a cohesive project that could be released.

I’m quite excited to have finally reached this point. As much as I’ve enjoyed developing specific skills, the parts of the course I’ve enjoyed so far is the work with a visible output. I have a feeling YelpCamp will be a big learning experience and something I’ll likely look back on as a reference in any future developments I do. A library of learnings if you will.

As with other parts of the course I intend to share my progress and code here as a form of public note-taking. I’ll create a specific public GitHub repo so you can follow along and see how the code changes over time. I expect this part of the course will likely take me about 6-8 weeks and will make up the vast majority of posts here over that time. Exciting!

JSON

I’ve worked with JSON data before when I was learning Swift and iOS development. At the time I didn’t really know much of JavaScript or its object syntax. Back then, I made my version of a weather app callClima“. That app pulled down weather information based on your current location using JSON data from a third party API.

Having worked with JavaScript for the past few months JSON seems a lot more obvious now. This morning I made a simple IMDB like web app pulling data from http://omdbapi.com/. I still don’t know how to deploying these node.js apps to a server somewhere. As soon as I do I’ll update posts like this linking to my work.

It’s amazing to me how easy it is to get something quite functional up and running with node.js. A few NPM packages, access to a good source of data from a third party API and you’re away.

Even finding details on APIs for basically anything is really easy thanks to services like https://www.programmableweb.com/. It’s basically just a giant directory of anything and everything you might want to know about publically available APIs.

Learning all this kind of makes me feel like I’m discovering coding superpowers. It’s so easy it feels like cheating.

A battle for the ages

Spaces vs tabs is a hilarious holy war that doesn’t really matter but I do find amusing. Modern text editors normalise differences on a project anyway, but I do love these ideological battles. The video below covers it better than I ever could. Enjoy… btw I’m a spaces man 😉

Express snippets

Since I’m playing with Express at the moment I’ve updated my collection of snippets to include a few things I seem to be doing regularly:

Express setup

I use the shortcut “myExpressSetup” to kick off all my new Express projects. It has basically all the parts I need to get the project underway, and means I dont have to remeber each bit. This is more or less the Express equivalent of the HTML boilerplate I’ve mentioned in the past.

  'Express Setup':
    'prefix': 'myExpressSetup'
    'body': """
    "use strict"

    var express = require( "express" );
    var app = express();
    var bodyParser = require( "body-parser" );

    app.use( bodyParser.urlencoded( {
      extended: true;
    } ) );

    app.set( "view engine", "ejs" );

    app.get( "/", function( request, response ) {
      response.render( "home" );
    } )

    $1

    app.get( "*", function( request, response ) {
      response.send( "404 page not found );
    } )

    // Tells express to listen for requests (Start server)

    app.listen( 3000, function() {
      console.log( "The server started on http://localhost:3000/" );
    } );
    """

Get route

Shocking no one, if there’s one thing I seem to be doing a lot it’s displaying content on an HTML page. Using the shortcut “myGetRoute” this one basically says if the URL fits this pattern take it and display it using the “project” template.

'Express Get':
    'prefix': 'myGetRoute'
    'body': """
    app.get( "/project/:thing", function( request, response ) {
      let thing = request.params.thing
      response.render( "project", {
        thingVar: thing
      } );
    } );
    """

So, for example, I could have a URL like geekpulp.co.nz/project/banana and have it populate the project templates H1 with “banana”. If the URL was then geekpulp.co.nz/project/apple it would populate the project template H1 with Apple. Obviously later in the course, I’ll start populating things from a database and this will all make a lot more sense.

Post route

This one’s all about collecting data from a form and adding it back to the page. Again this sort of thing will make a lot more sense when I’m actually posting the data to a database. At the moment if you refresh the page everything will just return to defaut values because nothing is being saved to the backend.

'Express Push':
    'prefix': 'myPushRoute'
    'body': """
    app.post( "/addteam", function( request, response ) {
      let newTeam = request.body.newTeam;
      teams.push( newTeam );
      response.redirect( "/teams" );
    } );
    """

These new snippets are certain to change in the next week or so as I expand to doing things that are actually useful. At this stage, a lot of this stuff is just code that facilities learning. Having said that, I find making snippets for repetitive tasks a good habit to form. Ultimately the whole point is being as efficient as possible.

NPM and a Demon

So learning node.js has been really interesting so far, I’m quite taken by it. It’s great being able to transfer my JavaScript skills from frontend to backend. One of the things I’m enjoying most about it is NPM. By their own definition:

NPM is the world’s largest software registry. Open source developers from every continent use NPM to share and borrow packages, and many organizations use NPM to manage private development as well.

Docs.npmjs.com. (2019). About npm | npm Documentation. [online] Available at: https://docs.npmjs.com/about-npm/ [Accessed 22 Mar. 2019].

So what does all this mean? As a developer, you have super easy access to a vast collection of code packages that can be used in your own projects or processes. One such package that’s saved me a repetitive task is nodemon. Nodemon does the simple job of restarting node every time I save a code change.

To install a package like nodemon you simply use the command:

npm install -g nodemon

In this command, the “-g” installs nodemon globally. This means it will work in all node projects. To run it use the command “nodemon” and your node server will fire up. Then each time you save a change to your code, bam, the server restarts to reflect the code change.

NPM has just shy of a million packages. So, if you’re in need of something to support a project, npmjs.com should be your first destination.

The three stages of developer growth

Today I happened across a great Quora post by Andreas Blixt. In the post, Andreas outlines the three stages of developer growth:

The novice hacker
“This code sure is ugly and I don’t quite understand why it works, but here it is!”
A developer in this phase finds creative solutions to problems, they just happen to be copy/pasted from all over the internet.
The problem is that the code breaks whenever it needs to be changed or it encounters a new situation

The philosophizing abstracter
“This code works for now, but if I move this part into a factory, and create an interface for these methods, it’ll also support all these future cases I can think of!”
They’ve read all the articles on how to structure code. There’s a lot of patterns out there for object-oriented (or functional!) code. Their code has all sorts of useful interfaces, abstraction layers, factories, extension methods, data structures.
Things break down when the project needs to move in an unexpected direction and it turns out that implementing the change requires changing a lot of the codebase because there were a bit too many abstractions that ended up creating hidden dependencies across the code.

The wise hacker
“Move fast. Break things. Revise and fix. Get shit done.”
Finally, developers balance the concepts of hacking and abstracting. They write code that simply solves the problem at hand, then they take a step back and look for repetition that can be simplified. They know from experience that things always change unexpectedly and that you can’t always foresee what will or will not be useful in the future.

Blixt, A. (2015, October 22). What are the growth stages of a programmer? Retrieved March 20, 2019, from https://www.quora.com/What-are-the-growth-stages-of-a-programmer/answer/Andreas-Blixt

As someone basically just starting out, I find this perspective from a senior developer very reassuring. I can absolutely see this path before me. Already I find myself writing code to get things working, then revisiting the code to see how I can improve it.

I’m yet to have the knowledge to do a great job on the improvements, but I feel that is my workflow. Having these stages outlined actually impacts my pathway through them. As I develop more skills I’ll be mindful of when and where I should deploy them.

How much time should I spend a day learning to code?

This is the question I asked myself when I first started learning (well relearning really) to code a while back. Obviously I Googled this and found some really interesting answers, but generally speaking, the common wisdom is about 4 hours a day.

I call bullshit on this. In my experience (and your mileage may vary) you should code every day, but it really doesn’t matter how much. Sure, you’ll get to the destination faster if you’re able to put 4 hours a day, but that number is completely unrealistic for a good chunk of the population.

Personally, I have a full-time job, 2 young kids, and a wife. 4 hours a day is an absurd amount of free time for me each day to work on much of anything. So when I study something I work to a very simple philosophy. Do some study every day. That’s it. Change is all about momentum. You don’t have to stop sleeping to make a change, you just have to show up every day and do something.

Routine is key

My personal routine is to get out of bed early, normally around 5-5:30am. Then I do as much of my course work before the kids get up as possible. Normally I’d get around an hour or so done. Some days I might sleep in and only get 30 mins, but I still progress. To solidify my learning, in the evening I go over what I did in the morning and write up notes. Sometimes those notes turn into a blog post. In that way, I’m sort of doing double duty. Sharing what I’ve learnt and documented it for my own retention all in one.

The thing about learning anything over and above your already busy life is that you have to make it accessible or it won’t work. Lowering the barrier to just do something each day means I might only research something I learnt the day before or add some comments to code I wrote a week ago. It really doesn’t matter what it is, as long as I get started each day.

So if you’re wanting to study but don’t think you have the time? Explore study options that let you learn in small chunks and to your own deadlines. I’ve found both Udacity and Udemy great services for this sort of thing. You won’t get a formal qualification from these services, but if you’re just interested in the skills then does that really matter?

I made a thing: Aroha generator

So I made another thing. The only reason I’m really even talking about it is because what’s going on in Christchurch is so awful I can hardly bear it. Making new things (even simple things like this) is a good distraction.

So in an effort to cheer myself up I made a web thing that says Aroha (and basically anything else I choose) as many times as I ask it to. I did this in node.js, which I’ve never used before, so this is a good learning experience. I have no idea how to deploy anything in node yet, but once I learn I’ll put it up somewhere.

Obviously this is not something I expect to be used in anyway, but learning new things always puts my mind in a good place. It makes me think about the good things in life like change, growth and the new. Something we all need to be thinking more about, especially in dark times. I’m certainly not a perfect person, but I try hard to be a better one every day. We owe that to ourselves, we owe it to each other.

Aroha Generator in all its glory!

If you’re interested in this very random project, check it out on GitHub. If you feel like escaping for a bit then you’re welcome to add to it, or create your own, or help someone else to create one. Just remember to be kind to one another.

Aroha.