Hacks to get Loopback App’s running on Heroku

To get Loopback App’s running on Heroku, a couple of hacks are required due to the way Loopback manages it’s configurations.

The first issue to solve is to get Loopback to take the port number from the environment since Heroku uses arbitrary port numbers to target different applications.

There is probably a more elegant way, however I first wanted to stay out of node_modules files, so I opted to just focus on modifying server.js.

It’s actually very easy, since loopback internally supports passing arguments to the listen function all the way out from the server.js file.
It does this by switching between automatic configuration and explicit configuration from the arguments passed to the listen function.

So, basically it’s just amending the “start” function to fetch the port number from the environment and pass it as argument to the listen function.

I have done it this way…

app.start = function () {
   // start the web server
   var port = process.env.PORT || 8000;
   return app.listen(port, function () {
      console.log('Web server listening at: %s', app.get('url'));

Using node-sass with latest version of io.js

When using node-sass together with io.js, one of the problems is that it’s not updated to work with the latest version of io.js (at the time of writing, v.1.6.2).
Currently node-sass only supports io.js until v.1.2.

This causes the installation of node-sass and any libraries that depends on node-sass to fail to fetch all resources during installation, which eventually causes processes that tries to use the node-sass library to fail.

The error message during installation is (in my case, using io.js 1.6 on a MBP) the following:

Can not download file from https://raw.githubusercontent.com/sass/node-sass-binaries/v2.0.1/darwin-x64-iojs-1.6/binding.node

The error message in the process trying to use node-sass:

Error: `libsass` bindings not found. Try reinstalling `node-sass`?

The failing part is when the node-sass library tries to fetch binding files which are necessary to connect to some of it’s libraries, the solution is therefore to either change the code where it fails (easy, but messy, not my recommendation) or simply download the binding files and manually add it to node-sass (easy, and also a bit messy, but at least we don’t change the code, so a bit easier to manage).

So, what I did, downloaded the file from here:

and save it to the following location:

Now, when you execute a process that uses node-sass, it will find the file, and even you are not running io.js 1.2, the binding still works (only tried with io.js 1.6).

It’s by no means a perfect solution, but it solves the problem immediately and it’s something easily automated since it only involves downloading and copying some files, not changing some code…


Simple WhoAmI for Loopback

Retrieving the currently authenticated user in a Loopback Application can be done in many ways, and one of them is the one I want to share in this post.

I wanted to be able to utilise the Angular SDK as well as the Explorer, so adding a routing manually in a boot script was not really an option, however simple that might be, so I decided to opt for implementing it as a custom Model.

The first thing to do is to create a whoami.json and whoami.js file in the commons directory.


  "name": "WhoAmI",
  "base": "Model",
  "plural": "whoami",
  "acls": [
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"


module.exports = function (WhoAmI) {

    WhoAmI.whoAmI = function (req, next) {
        var AccessToken = WhoAmI.app.models.AccessToken;
        AccessToken.findForRequest(req, {}, function (aux, accesstoken) {
            var UserModel = WhoAmI.app.models.User;
            UserModel.findById(accesstoken.userId, function (error, user) {
                next(error, user);

            accepts: {arg: 'req', type: 'object', http: {source: 'req'}},
            returns: {arg: 'user', type: 'object'},
            http: {path: '/', verb: 'get'}

Secondly, I make sure to add the model to the model-config.json

"WhoAmI": {
  "dataSource": null,
  "public": true

Now, when you restart the server, you will see the following endpoint having been added to your API


And because of the ACL’s we set in the model, all requests without an access-token are handled by the security of Loopback… so there you go, a nice and easy WhoAmI !

Alternatively… a bootscript

If you for some reason should be inclined to prefer putting it in a boot script, all you have to do is to create a file in the boot directory of the server with the following content

module.exports = function (server) {

    var router = server.loopback.Router();

    router.get('/whoami', function (req, res) {

        var AccessToken = server.models.AccessToken;
        AccessToken.findForRequest(req, {}, function (aux, accesstoken) {
            if (accesstoken == undefined) {
                    'Error': 'Unauthorized',
                    'Message': 'You need to be authenticated to access this endpoint'
            else {
                var UserModel = server.models.User;
                UserModel.findById(accesstoken.userId, function (err, user) {


Gist: https://gist.github.com/pmoelgaard/af6aa61146766f0e8551


Sequelize for Dash

While getting one’s hands dirty with Sequelize, it can be useful to have access to the documentation, so even it’s relatively simple I decided to wrap the documentation for Sequelize in a docSet for Dash…


Here is the link for the feed file…
Direct Link

…and here is the link for the custom URL for adding the feed to your Dash…
Add Feed Automatically



Bookshelf for Dash

In addition to the recent Moment and Async docSet for Dash, I have created a third docSet distribution, this time for the cool library Bookshelf which is pretty cool Node.js ORM for PostgreSQL, MySQL and SQLite3 in the style of Backbone.js.


Created by Tim Griesser, its just one project in the line of many which is solving every day issues with elegance and efficiency… Currently its the same approach as with Moment and Async, where its merely an embedded version of the online documentation…


Add the documentation from here… Direct Link