Dwight Watson's blog

Private functions in Sails.js

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

In modern frameworks you are usually able to define controller functions as being public or private, indicating whether or not they are callable from the browser. You might use private functions to encapsulate certain functionality or just to keep certain parts of your application inaccessible from the public. In Sails.js, it isn't as clear as it might be elsewhere as to how to create private functions.

module.exports = {
index: function (req, res) {
privateFunction();
anotherPrivateFunction();
},
};

function privateFunction() {
console.log("This function can only be called from within the controller!");
}

var anotherPrivateFunction = function () {
console.log(
"This function can also only be called from within the controller!"
);
};

Now you can implement private functions in your own Sails.js controllers!

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.