如何在节点js上发出HTTP POST请求

问题描述 投票:0回答:1

以下是服务器:

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帖子中获取此值。我怎样才能做到这一点?

node.js rest api http post
1个回答
0
投票

使用请求库:

var request = require('request');

request.post(
    'http://www.example.com',
    { json: { key: 'value' } },
    function (error, response, body) { 
            console.log(body)
    }
);
© www.soinside.com 2019 - 2024. All rights reserved.