node調用python服務

const http = require('http');

function callPhoenixApi(filter,path) {
return new Promise((resolve,reject) => {
const post_data = JSON.stringify(filter);

const options = {
// host: '100.90.112.17',
host: '10.95.156.249',
port: '5000',
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data, 'utf8')
}
};

const req = http.request(options, function(res) {
let c = '';
res.on('data', (chunk) => {
c = chunk;
});
res.on('end', () => {
resolve(c.toString());
});
});

req.on('error', (e) => {
reject(`${e.message}`);
});

req.write(post_data);
req.end();

})
}

async function getStationInfoByOrder(B, ctx, next) {
let filter = ctx.request.body;
let path = '/getStationInfoByOrder';
await callPhoenixApi(filter, path).then(res => {
ctx.body = JSON.stringify(res);
});
let res = ctx.response;
}

async function getStationInfoById(B, ctx, next) {
let filter = ctx.request.body;
let path = '/getStationInfoByStationId';
try {
let result = await callPhoenixApi(filter,path);
ctx.body = JSON.stringify(result);
} catch (err) {
}
}

async function getStationInfoByName(B, ctx, next) {
let filter = ctx.request.body;
let path = '/getStationInfoByStationName';
try {
let result = await callPhoenixApi(filter,path);
ctx.body = JSON.stringify(result);
} catch (err) {
}
}

async function getStationInfoByRec(B, ctx, next) {
let filter = ctx.request.body;
let path = '/getStationInfoByRec';
try {
let result = await callPhoenixApi(filter,path);
ctx.body = JSON.stringify(result);

} catch (err) {
}
}


module.exports = {
getstationinfobyorder: {
method: 'POST',
fn: getStationInfoByOrder
},
  querybyid: {
    method: 'POST',
    fn: getStationInfoById
  },
querybyname: {
   method:'POST',
fn:getStationInfoByName
},
getstationinfobyrec:{
   method:'POST',
fn:getStationInfoByRec
},
};

發佈了97 篇原創文章 · 獲贊 16 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章