14

I have created a web app with node.js, express, and angular.js in the past. I am starting a new project and I want to also use MongoDB. That would be the MEAN stack. Using just MEAN, I could start a project with this: http://mean.io/.

Now, I have written REST API's in the past and I have heard about sails.js which sounds very compelling. It can automatically create REST API's for you.

So my question is, what steps would I follow to start a new project with the MEAN stack AND sails.js?

Options:

  1. Would I clone the mean.io stack, run the npm install and then also npm install sails.js?
  2. Or, it seems like sails.js has it's own idea of what to do for a directory structure. So would I install sails.js as per their instructions http://sailsjs.org/#!getStarted and then npm install Angular and Mongo? (I think I would not need Mongoose since sails.js has its own ORM, waterline).

I am actually going to try option 2 today, but I would be very glad to know what steps have worked for others.

Thank you very much!

2
  • I did a google search for this and did not come up with much, so I figured we need to start the discussion here.
    – Jess
    Jan 31, 2014 at 16:53
  • So far I just did npm install -g sails and sails new myproj.
    – Jess
    Jan 31, 2014 at 18:54

2 Answers 2

14

You are on the right path with npm install -g sails and sails new myproj. Since you want to use mongo, you will need to install the waterline adapter for mongo (in project dir) npm install sails-mongo --save and configure sails to use mongo.

Add the mongo config to the config/adapters.js file:

module.exports.adapters = {
  'default': 'mongo',

  mongo: {
    module   : 'sails-mongo',
    host     : 'localhost',
    port     : 27017,
    user     : 'username',
    password : 'password',
    database : 'your mongo db name here',

    // OR
    module   : 'sails-mongo',
    url      : 'mongodb://USER:PASSWORD@HOST:PORT/DB',

    // Replica Set (optional)
    replSet: {
      servers: [
        {
          host: 'secondary1.localhost',
          port: 27017 // Will override port from default config (optional)
        },
        {
          host: 'secondary2.localhost',
          port: 27017
        }
      ],
      options: {} // See http://mongodb.github.io/node-mongodb-native/api-generated/replset.html (optional)
    }
  }
};

Additionally, to create your API, (in the project dir) use sails generate NAME where NAME is the name of the model. By default, anything can be added to the database, so you may want to limit the properties/fields and possibly even validate them. Its easy. The generate command created a few files for you, one of which is models/NAME.js. In this file you can simply export an object with the attributes corresponding to the field you want and any restrictions/validations you want to happen before its saved.

// Person.js
var Person = {
  attributes: {
    firstName: 'STRING',
    lastName: 'STRING',
    age: {
      type: 'INTEGER',
      max: 150,
      required: true
    }
    birthDate: 'DATE',
    phoneNumber: {
      type: 'STRING',
      defaultsTo: '111-222-3333'
    }
    emailAddress: {
      type: 'email', // Email type will get validated by the ORM
      required: true
    }
  }
};

module.exports = Person;

This page lists all of the different types and validations you can have.

Once you are all set up, run sails lift to start your server. The default port is 1337, but you can change that with the PORT env var or in your local configs

module.exports = {
    port: 80
    // ... more config things
}

Also, as for the 'A' in MEAN, check out Angular Sails. Its a small angular service to let you easily take advantage of the socket.io things that sails is doing for you. You can call all of your APIs over the socket connection to make them even lighter and faster.

In this case $sails replaces $http

app.controller("FooController", function ($scope, $sails) {
    $scope.bars = [];

    $sails.get("/bars", function (data) {
      $scope.bars = data;
    });
});
2
  • This is awesome! Thank you. I will follow this along in the next few days.
    – Jess
    Feb 3, 2014 at 13:43
  • However the sails mongo adapter at the time of writing has issues with respect to associations n many times require access to the native connection for querying sub documents.. You might want to check sails mongo issues to see how it compares to mongoose.. IMO nothing to beat mongoose as yet but I still went ahead with it
    – ajeeshpu
    Feb 14, 2014 at 15:36
0

There's an awesome boilerplate project here that uses react.js instead of angular: https://github.com/lynnaloo/yacht-rock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.