dim*_*_0n 2 command-line-interface sails.js
提起我的应用程序后,我需要立即运行PostgreSQL脚本。例如,我需要psql -d DOGHOUZ -a -f script.sql从应用程序执行以下命令:有办法吗?
这取决于您如何提升应用程序。如果您使用:
node app.js
Run Code Online (Sandbox Code Playgroud)
你可以加
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
Run Code Online (Sandbox Code Playgroud)
在您的app.js文件中,在sails.lift(rc('sails'))行之前;
否则,您需要将其添加到配置中。最好的方法是在/ config中创建新文件,例如/config/eventhooks.js,并包含以下内容:
module.exports.eventhooks = function(cb) {
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里阅读更多:
编辑1
要执行CLI命令,只需执行以下操作:
var exec = require('child_process').exec;
var cmd = 'psql -d DOGHOUZ -a -f script.sql';
exec(cmd, function(error, stdout, stderr){
// command output is in stdout
});
Run Code Online (Sandbox Code Playgroud)
有关在CLI中执行命令的更多信息,请参见此处