如何在节点js上发出HTTP POST请求 [英] How to make an HTTP POST request on node js

查看:129
本文介绍了如何在节点js上发出HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是服务器:

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

The server above has been created using that package adding middlewares https://www.npmjs.com/package/json-server The number 52 in the function "sendEvent" is an example. I have to take this value from an http post. How can I do that?

推荐答案

使用请求库:

var request = require('request');

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

这篇关于如何在节点js上发出HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆