npm install sails --global
sails new yourapp
cd yourapp; sails generate api user
在api/models目录下,定义User模型
module.exports = {
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true
},
name: 'string',
city: 'string'
}
};
在api/controllers目录下,定义controller中的action
module.exports = {
getUserInfo: function (req, res) {
User.find({id:1}).exec(function(err,user){
if(err){
return res.json({data:"no data"});
}else{
return res.json(user);
}
});
}
};
module.exports = {
host: '0.0.0.0',
port: process.env.PORT || 8080,
environment: process.env.NODE_ENV || 'development'
};
module.exports.connections = {
mysql: {
adapter: 'sails-mysql',
user: 'root',
host: '0.0.0.0',
database: 'c9'
},
};
module.exports.models = {
connection: 'mysql',
migrate: 'alter'
};
module.exports.routes = {
'/': {
view: 'homepage'
},
'/user':"UserController.getUserInfo"
};
sails lift