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?
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!
Create your app You'll see a prompt to choose your project template: Choose a template for your new Sails app: 1. Web App · Extensible project with auth, login, & password recovery 2. Empty · An empty Sails app, yours to configure (type "?" for help, or <CTRL+> to cancel) ?
Creating a new Sails application 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.
js apps in a matter of weeks, not months. Sails is the most popular MVC framework for Node. js, designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture.
Adding middleware To configure a new custom HTTP middleware function, add a middleware function as a new key in middleware (e.g. "foobar"), then add the name of its key ("foobar") in the middleware. order array, wherever you'd like it to run in the middleware chain.
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;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With