【问题标题】:How to make an HTTP POST request on node js如何在节点 js 上发出 HTTP POST 请求
【发布时间】:2018-02-28 23:55:26
【问题描述】:

以下是服务器:

const Bpmn = require('bpmn-engine');

const processXml = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <process id="theProcess" isExecutable="true">
    <startEvent id="start" />
    <exclusiveGateway id="decision" />
    <endEvent id="end1" />
    <endEvent id="end2" />
    <sequenceFlow id="flow1" sourceRef="start" targetRef="decision" />
    <sequenceFlow id="flow2" sourceRef="decision" targetRef="end1">
      <conditionExpression xsi:type="tFormalExpression" 
language="JavaScript"><![CDATA[
      this.variables.input <= 50
      ]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="decision" targetRef="end2">
      <conditionExpression xsi:type="tFormalExpression" 
language="JavaScript"><![CDATA[
      this.variables.input > 50
      ]]></conditionExpression>
    </sequenceFlow>
  </process>
</definitions>`;

const engine = new Bpmn.Engine({
  name: 'exclusive gateway example',
  source: processXml
});

engine.once('end', (definition) => {
  if (definition.getChildActivityById('end1').taken) throw new Error('<end1> 
was not supposed to be taken, check your input');
  console.log('TAKEN end2', definition.getChildActivityById('end2').taken);
});

function sendEvent(value){
    engine.execute({
  variables: {
    input: value
  }
}, (err, definition) => {
  console.log('Bpmn definition definition started with id', 
definition.getProcesses()[0].context.variables.input.value);
  console.log('sent event' + value);
  console.log(engine.getState())
});
}

i = 0;
//hello.js
module.exports = (req, res, next) => {
  //res.header('X-Hello', 'World')
  //console.log(req);
  if(!i++){
      sendEvent(52);
  }
  console.log(engine.getState())
  next()
}

上面的服务器是使用添加中间件的那个包创建的 https://www.npmjs.com/package/json-server 函数“sendEvent”中的数字 52 就是一个示例。我必须从 http 帖子中获取这个值。我怎样才能做到这一点?

【问题讨论】:

  • 您可以使用 NPM 上的 request 模块向外部主机进行 POST。

标签: node.js rest api http post


【解决方案1】:

使用请求库:

var request = require('request');

request.post(
    'http://www.example.com',
    { json: { key: 'value' } },
    function (error, response, body) { 
            console.log(body)
    }
);

【讨论】:

  • 感谢您的回答。那么如何获取和处理服务器上的“价值”呢?它是我在函数 sendEvent 中使用的数字或字符串
  • 对不起,我希望能帮上忙,但我对 node.js 一无所知。我真的只是用谷歌搜索了这个答案。点赞?
猜你喜欢
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2017-08-19
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多