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

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


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


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