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


AppJS… Build Cross-Platform Applications

…for desktop using HTML, CSS, Javascript !

It’s still very early stuff, however its interesting to see how a lightweight container like this may develop into something pretty useful, at least the stack of technologies used is entirely aligned with how I would set out to build a lightweight app container…

Why AppJS?

Because it is simple and yet powerful. Using AppJS you don’t need to be worry about coding cross-platform or learning new languages and tools. You are already familiar with HTML, CSS and Javascript. What is better than this stack for application development? Beside, AppJS uses Chromium at the core so you get latest HTML 5 APIs working. So relax and focus on the task your application should do.


AppJS allows you to use HTML 5 APIs to create attractive applications from Word Processors to 3D Games. You are no longer limited to default GUI widgets that plaforms force you to use. Creating custom UIs is now only limited to your imagination!


Using CSS you can decorate widgets as you like. Create a custom widget in HTML and complete your work with decorating it. Adding shadows, animating elements and transforming objects in 3D space are a few examples of what you can do with CSS 3.


The interesting part of AppJS is that it uses Node.js as the backbone. Node.js has been built to ease the process of developing scalable network applications. But today, you can see Node nearly everywhere! It has a nice API and lots of modules.

Check it out…

Arbitrary Thoughts

Introducing NPM…and a couple of comments

npm is a package manager for node. You can use it to install and publish your node programs. It manages dependencies and does other cool stuff.

Check it out…

However, there are a few comments I would like to attach, and that is e.g. for the installation…

It states on the npm website, that you can execute a one-line install using cURL by entering the following cURL command…

curl http://npmjs.org/install.sh | sh

However, if you attempt to do this without using SUDO, you might end up with the following error…

Error: EACCES, Permission denied ‘/usr/bin/npm’

So, to make life easier for yourself, add SUDO to the SH part of the cURL command, so it looks like this, and you will be fine…

curl http://npmjs.org/install.sh | sudo  sh

Secondly,  In mac OSX 10.7 compilation fails if you use “make -j n” command , you can use “make”.
Also, if you get some complaints that make command is not found, its probably because xCode (or another make tool) isn’t installed, easiest way around that is to simply install xCode from AppStore…