我需要在提升我的应用程序后立即运行 PostgreSQL 脚本。例如,我需要从应用程序执行此命令:psql -d DOGHOUZ -a -f script.sql
。有办法做到这一点吗?
最佳答案
这取决于您提升应用的方式。如果您使用:
node app.js
可以添加
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
在 app.js 文件中行 sails.lift(rc('sails')); 之前
否则您需要将其添加到配置中。最好的方法是在/config 中创建新文件,例如/config/eventhooks.js,内容如下:
module.exports.eventhooks = function(cb) {
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
}
您可以在这里阅读更多内容:
编辑 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
});
有关在 CLI 中执行命令的更多信息,您可以 read here
关于sails.js - 在 Sails.js 应用程序中运行 CLI 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469607/