em đang học node thử viết 1 đoạn để curl show html của 1 trang web
api.js
var http = require("http");
var Service = require("./service");
http
.createServer(function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "*");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
res.setHeader("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return;
}
var service = new Service();
var response = service._execute('https://vip.hacklike.me');
res.end(response); // empty
})
.listen(8080);
service.js
var Curl = require('node-libcurl').Curl;
class Service {
constructor() {
this.curl = new Curl();
}
_execute(url, method) {
var curl = this.curl;
var html = '';
curl.setOpt('URL', url);
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt('SSL_VERIFYPEER', false);
curl.setOpt('SSL_VERIFYHOST', false);
curl.on('end', function (statusCode, body, headers) {
console.log(body); // log full html ok
html = body;
this.close();
});
curl.on('error', curl.close.bind(curl));
curl.perform();
return html; // empty
};
}
module.exports = Service;
khi em run node api , rồi chạy ip:8080 thì ở console.log terminal hiện đầy đủ html của trang web, nhưng ở trình duyệt lại trắng trang, em nghĩ là do nó không đồng bộ, có cách nào fix không ạ, e cảm ơn