当我尝试使用此代码从 nodejs 中发布一个实例到环回时,我没有收到任何错误,但也没有发布任何数据?

//NPM Package (request)
var request = require('request');

// Address of Loopback API on the same server
var api = "http://localhost:3000/api/devices";

//JSON Construction
var deviceInstance = {
     "manufacturer": "manufacturer",
     "model": "model"
   //etc
}

// NPM (request)
request({
   url: api,
   method: "POST",
   headers: {"Accept: application/json"},
   json: true,
   body: deviceInstance
}, function (error, response, body) {
      if(error) {
        console.log('error: '+ error);
      } else {
        console.log('document saved to api')
        console.log(body);
        console.log(response);
      }
});

process.exit();

我没有从同一台机器的服务器得到任何响应或错误。如果我在 postman (Windows 应用程序)中尝试相同的调用,它实际上会在 API 中创建一个实例,那么为什么我的本地 Node 没有连接到 API?

最佳答案

为什么 process.exit()

调用 process.exit() 将强制进程尽快退出,即使仍有异步操作未决。

关于javascript - 尝试使用请求 NPM 从 NodeJS 发布数据到本地主机(Loopback Swagger API),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54530153/

10-09 20:15
查看更多