Make a GET Request in Nimrod

In this code snippet we make a GET request to Google’s Maps API. We’ll use the strutils module to handle some of the strings.

I couldn’t get anything to work until I updated my Nimrod installation to 0.11.2 or something. I had 0.10.? before and I had zero luck with this. I think I read there was a bug in httpclient. So if you’re having trouble, make sure you’ve installed the latest (stable) compiler.

import httpclient
import strutils

proc mapreq( lat, lon: string ) =
    var coord = strutils.join( [ lat, lon ], "," )
    coord = strutils.join( [ coord, "sensor=false" ], "&" )
    var address = "http://maps.google.com/maps/api/elevation/json?locations="
    var call = strutils.join( [ address, coord ], "" )
    var res = httpclient.get( call )
    echo( res.body )

mapreq( "32.0", "-102.0" )

Then compile and run this as,

nim c -r coord.nim

And you should see this in your terminal,

{
   "results" : [
      {
         "elevation" : 836.9058837890625,
         "location" : {
            "lat" : 32,
            "lng" : -102
         },
         "resolution" : 9.543951988220215
      }
   ],
   "status" : "OK"
}

Handle EADDRINUSE Error in Node

When a node application crashes unexpectedly, you might get a EADDRINUSE, Address already in use error when you try to restart it. This means that the port you’re using is already being used by the crashed application. What you need to do is fun the PID of that process, and then kill it. There’s more than one way to skin that cat, but I like to use netstat. N.B. You’ll need to run this as sudo for it to work.

Continue reading Handle EADDRINUSE Error in Node

Using tmux

I found this great tutorial on tmux by Daniel Miessler that was designed to take you from “wtf tmux” to “omg tmux” with extreme haste. I’m reproducing parts of that blog post here because I find myself googling it at least three times a day. (Because it’s that useful.) Briefly, tmux is a utility that allows you to connect to a remote server, start a job, and then log off without quitting your job. When you log back in from another location, you can access that job again through tmux. The screen utility also does this, but not as well. For instance, if you have a really long username, then you might not be able to start screen, because edge cases.

Continue reading Using tmux