node调用python服务

来源:互联网 发布:淘宝网历任总裁 编辑:程序博客网 时间:2024/05/16 12:14
const http =require('http');

function callPhoenixApi(filter,path) {
return newPromise((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 functiongetStationInfoByOrder(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 functiongetStationInfoById(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 functiongetStationInfoByName(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 functiongetStationInfoByRec(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
},
};

原创粉丝点击