내 sails.js 앱이 PHP 프로젝트에서 임베디드되어 있습니다. php는 돛 모델에 대한 일부 날짜를 생성합니다. beforeCreate 콜백 또는 일부 맞춤 메소드에서이 날짜를 가져 오는 방법을 찾을 수 없습니다. 두 모델 (PHP에서 돛을 & 모델에서)를 동기화하는 DB를 사용하고 싶지 않아요. 그리고 나는 원격 PHP 응용 프로그램에 날짜를 보내야합니다.sails.js : 원격 서버에서 모델 데이터 가져 오기
module.exports = {
index: function (req, res) {},
create: function (req, res) {
if (!req.param('login'))
throw new Error('Login is undefined');
if (!req.param('message'))
throw new Error('Initial message is undefined');
var user, thread;
User.create({
id: 1,
name: req.param('login'),
ip: req.ip
}).done(function (err, model) {
user = model;
if (err) {
return res.redirect('/500', err);
}
user.fetch(); // my custom method
});
return res.view({ thread: thread });
}
};
및 모델 : :
module.exports = {
attributes: {
id: 'integer',
name: 'string',
ip: 'string',
fetch: function (url) {
var app = sails.express.app;
// suggest this but unlucky :)
app.get('/path/to/other/loc', function (req, res, next) {
console.log(req, res)
return next()
});
}
}
};
UPD 내 솔루션
모델 :
sails.js 여기
내 컨트롤러 v.0.9.xbeforeCreate: function (values, next) {
var options = 'http://localhost:1337/test',
get = require('http').get;
get(options, function (res) {
res.on('data', function (data) {
_.extend(values, JSON.parse(data.toString()));
next();
});
}).on('error', function() {
throw new Error('Unable to fetch remote data');
next();
});
}
답장을 보내 주셔서 감사합니다. 내 방식은 require ('https')와 유사합니다. – shifteee