How to start a new project with MEAN and sails.js

angularjs, mongodb, node.js, sails.js, waterline

Solution

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;
    });
});

Problem

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: - Would I clone the mean.io stack, run the npm install and then also npm install sails.js? - 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!

Original source