这里说下几个基本的文件配置
1
config/local.js
这个文件是本地环境配置
config/local.js的配置:
port: process.env.PORT || 1339, environment: process.env.NODE_ENV || ‘development‘, // The runtime "environment" of your Sails app is either ‘development‘ or ‘production‘.
第一个是运行项目的端口
第二个是运行模式
在开发模式下,运行会有报错提示,发布模式则会忽略这些
详细的说明在local.js的底部注释中
2
config/adapters.js
本地数据库配置
我这里用的是mysql
module.exports.adapters = { // If you leave the adapter config unspecified // in a model definition, ‘default‘ will be used. // Persistent adapter for DEVELOPMENT ONLY // (data is preserved when the server shuts down) ‘default‘: ‘mysql‘, disk: { module: ‘sails-disk‘ }, // MySQL is the world‘s most popular relational database. // Learn more: http://en.wikipedia.org/wiki/MySQL myLocalMySQLDatabase: { module: ‘sails-mysql‘, host: ‘YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS‘, user: ‘YOUR_MYSQL_USER‘, // Psst.. You can put your password in config/local.js instead // so you don‘t inadvertently push it up if you‘re using version control password: ‘YOUR_MYSQL_PASSWORD‘, database: ‘YOUR_MYSQL_DB‘ }, mysql: { module: ‘sails-mysql‘, host: ‘127.0.0.1‘, port: 3306, user: ‘root‘, password: ‘pwd‘, database: ‘dbname‘ } /* // OR (explicit sets take precedence) *//*module : ‘sails-mysql‘, url : ‘mysql2://USER:PASSWORD@HOST:PORT/DATABASENAME‘*//* }*/ };
这里的mysql配置是用到了sails-mysql组件
这个模块是来自于waterline组件的扩展
当然waterline是不需要再安装的了
只要在项目文件夹中先安装sails-mysql即可
cnpm install sails-mysql
3
config/cors.js
看这个名字就应该知道,跨域配置
//对所有的路由链接配置跨域 allRoutes: true, // Which domains which are allowed CORS access? // This can be a comma-delimited list of hosts (beginning with http:// or https://) // or "*" to allow all domains CORS access. //对所有的IP地址开放跨域 origin: ‘*‘, //允许cookie域跨域中传递 // Allow cookies to be shared for CORS requests? credentials: true, //这些不用解释了吧? // Which methods should be allowed for CORS requests? This is only used // in response to preflight requests (see article linked above for more info) methods: ‘GET, POST, PUT, DELETE, OPTIONS, HEAD‘, // Which headers should be allowed for CORS requests? This is only used // in response to preflight requests. headers: ‘content-type‘
4
跨域加密
如果是开放型项目,需要对第三方开放的,就需要加密咯
这里对跨域加密也有配置
config/csrf.js
文件内容
module.exports.csrf = false;
我这个项目中是没有加密的,也就不对外开放
5
session配置
config/session.js
文件内容
secret: ‘密钥字符串可以任意自定义‘, cookie: { maxAge: 4*60*60*1000//时间长度 }
6
routes路由配置
config/routes.js
这个可以自定义控制器和action函数的域名
‘message/inbox‘: //当然这个key可以自定义的 { controller: ‘MessageController‘, action: ‘inbox‘ }
这样...就酱紫吧...
7
还有个文件可能在默认生成项目的时候没有出现的
config/application.js
文件内容
module.exports={ port:1333, appName:"title标题", successRe:{"code":"0","msg":"success"}, errRe:{"code":"043","msg":""} };
像这样简单的配置,使用方法:
sails.config.appName 这样就可以整个项目全局调用了
相当于常量文件
Sails.js 教程 EP2,布布扣,bubuko.com
原文地址:http://my.oschina.net/code33/blog/290890