angularjs - How to start a new project with MEAN and sails.js -
i have created web app node.js, express, , angular.js in past. starting new project , want use mongodb. mean stack. using mean, start project this: http://mean.io/.
now, have written rest api's in past , have heard sails.js sounds compelling. can automatically create rest api's you.
so question is, what steps follow start new project mean stack , sails.js?
options:
- would clone mean.io stack, run npm install , npm install sails.js?
- or, seems sails.js has it's own idea of directory structure. install sails.js per instructions http://sailsjs.org/#!getstarted , npm install angular , mongo? (i think not need mongoose since sails.js has own orm, waterline).
i going try option 2 today, glad know steps have worked others.
thank much!
you on right path npm install -g sails
, sails new myproj
. since want use mongo, need install waterline adapter mongo (in project dir) npm install sails-mongo --save
, configure sails use mongo.
add mongo config 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 // override port default config (optional) }, { host: 'secondary2.localhost', port: 27017 } ], options: {} // see http://mongodb.github.io/node-mongodb-native/api-generated/replset.html (optional) } } };
additionally, create api, (in project dir) use sails generate name
name
name of model. default, can added database, may want limit properties/fields , possibly validate them. easy. generate command created few files you, 1 of models/name.js
. in file can export object attributes corresponding field want , restrictions/validations want happen before 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 validated orm required: true } } }; module.exports = person;
this page lists of different types , validations can have.
once set up, run sails lift
start server. default port 1337, can change port env var or in local configs
module.exports = { port: 80 // ... more config things }
also, 'a' in mean, check out angular sails. small angular service let take advantage of socket.io things sails doing you. can call of apis on socket connection make them lighter , faster.
in case $sails
replaces $http
app.controller("foocontroller", function ($scope, $sails) { $scope.bars = []; $sails.get("/bars", function (data) { $scope.bars = data; }); });
Comments
Post a Comment