内部app.post内的HTTPS请求不起作用

时间:2018-03-31 09:09:57

标签: node.js express https bots

我遇到问题

  

https.get(urlDosen,res => {......});

没有像你在这里看到的那样工作:

function handleEvent(event) {
  if (event.type !== 'message' || event.message.type !== 'text') {
    // ignore non-text-message event
    return Promise.resolve(null);
  }

  const b = String(event.message.text);
  if(b.substring(0,5)=='dosen'){
    const namaDosen = b.substring(6);
    const urlDosen = url+namaDosen;
    https.get(urlDosen,res => {
        console.log(res.headers['content-type']);

代码如下:

app.post('/callback', line.middleware(config), (req, res) => {
  Promise
    .all(req.body.events.map(handleEvent))
    .then((result) => res.json(result))
    .catch((err) => {
      console.error(err);
      res.status(500).end();
    });
});

// event handler
function handleEvent(event) {
  if (event.type !== 'message' || event.message.type !== 'text') {
    // ignore non-text-message event
    return Promise.resolve(null);
  }

  const b = String(event.message.text);
  if(b.substring(0,5)=='dosen'){
    const namaDosen = b.substring(6);
    const urlDosen = url+namaDosen;
    https.get(urlDosen,res => {
        console.log(res.headers['content-type']);
        if(res.headers['content-type']=='application/json; charset=UTF-8'){
          res.setEncoding('utf8');
          let body = '';
          res.on('data', data=>{
            body += data;
          }); 
          res.on('end', ()=>{
            body = JSON.parse(body);
            if(body['hasil']=='sukses'){
              const echo = {type:'text',text: 'Nama Dosen: ' + body['nama'] + '  Status: ' + body['status']};
              return client.replyMessage(event.replyToken, echo);}
            else{
              const echo ={type:'text',text:body['status']};
              return client.replyMessage(event.replyToken, echo);
              }
            }
          );
        } else{
          const hasil={type:'text',text:'Mohon mengulang kembali'};
          return client.replyMessage(event.replyToken, hasil);
          // message.channel.send("Mohon mengulang kembali");       
        }
      });


  } else{
  // create a echoing text message
  const echo = { type: 'text', text: 'salahnya dimana?' };

  // use reply API
  return client.replyMessage(event.replyToken, echo);
  }
}

我正在使用node.js.感谢

0 个答案:

没有答案