使用https模块进行远程请求
npm install https
npm install request
进行远程请求前先拼装好请求数据
var url = '/otn/leftTicket/query?leftTicketDTO.train_date='+req.body.date+'&leftTicketDTO.from_station='+req.body.start+'&leftTicketDTO.to_station='+req.body.end+'&purpose_codes=ADULT';
//生成GET请求的参数
var options = {
hostname: 'kyfw.12306.cn',
port: 443,
path: url,
method: 'GET',
rejectUnauthorized: false
};
进行远程请求
https.request(options, function(response){
var str = '';
//接收数据
response.on('data', function(chunk){
str += chunk;
});
response.on('end', function(){
//处理数据
response.setEncoding('utf8');
console.log(str);
});
});