Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What services would one add to the api/services folder in sails.js

Tags:

sails.js

I have a question consisting of two parts:

  1. What are the types of services one would add to the api/services folder in a sails.js app.
  2. How would one 'wire' those services to the rest of the application.

Thanks,

TM

like image 630
tmueller Avatar asked Aug 26 '13 15:08

tmueller


People also ask

What is sail service?

Services are stateless libraries of functions that you can use from anywhere in your Sails app. For example, you might have an EmailService which tidily wraps up one or more utility functions so you can use them in more than one place within your application.

How do you use sails in JavaScript?

To create a new Sails application, run the sails new command passing in the name of your application. This command will both generate a new Sails app and run npm install to install all dependencies for you.


1 Answers

a service would be in my opinion, a piece of logic that you need in multiple locations of your app. for example an email service. the following is taken directly from the sails-wiki github page.

// EmailService.js - in api/services
exports.sendInviteEmail = function(options) {

var opts = {"type":"messages","call":"send","message":
    {
        "subject": "YourIn!",
        "from_email": "[email protected]",
        "from_name": "AmazingStartupApp",
        "to":[
            {"email": options.email, "name": options.name}
        ],
        "text": "Dear "+options.name+",\nYou're in the Beta! Click <insert link> to verify your account"
    }
};

myEmailSendingLibrary.send(opts);
};

The wiring is done by sails itself:

// Somewhere in a conroller
 EmailService.sendInviteEmail({email: '[email protected]', name: 'test'});
like image 52
snyx Avatar answered Oct 03 '22 08:10

snyx