2017-04-15 48 views
0

你如何解决这个*?node.js requestify似乎什么都不做

对不起,但它是如此令人失望什么时候简单的东西不工作的方式,它声称他们应该。

以下是直接从projetc的gitHub网站截取。

如果我在PostMan中做同样的事情 - 没问题。

第一个console.log()工作。但是,以下的任何一个被调用。 看起来.then()s和.fail()会被调用。如果我添加一个catch()也不会被调用。

我确定在不同的node.js Express web应用程序中工作时没有问题。此应用程序是Express Web应用程序根目录中存在的node.js控制台应用程序(单个.js文件)。

这个.js文件在编译或执行时不会引发任何错误。

console.log("Let's begin"); 

/* appears to do nothing */ 
requestify.get('https://www.example.com/api/get.json') 
.then(function (response) { 
    console.log('response', response.getBody()); 
}) 
.fail(function (response) { 
     console.log('response Error', response.getCode()); 
    }) 
; 


/* also appears to do nothing */ 
requestify.request('https://www.example.com/api/get.json', { 
    method: 'GET' 
}) 
.then(function(response) { 
    console.log('responsebody', response.getBody()); 
    console.log('response headers',response.getHeaders()); 
    console.log('responseheader Accept', response.getHeader('Accept')); 
    console.log('response code', response.getCode()); 
    console.log('responsebody RAW', response.body); 
    }) 
.fail(function (response) { 
     console.log('response Error', response.getCode()); 
    }) 
; 
+0

您是否需要该模块? 'var requestify = require('requestify'); ' – Badacadabra

+0

编译或运行时没有错误。如果我没有发生错误“'requestify'没有定义”。但是,是的。感谢您的询问。 – Steve

+0

我测试了你的代码,它工作得很好。你在第一个'console.log'中只有一个错字。请使用双引号(“”)并再次测试。 – Badacadabra

回答

0

两件事情,

  1. 你没有requestify要求,
  2. 您的第一个控制台日志3个单引号。所以这肯定会打破。

'use strict'; 
 

 
const requestify = require('requestify'); 
 

 
/* appears to do nothing */ 
 
requestify.get('https://www.example.com/api/get.json') 
 
    .then(function (response) { 
 
    console.log('response', response.getBody()); 
 
    }) 
 
    .fail(function (response) { 
 
    console.log('response Error', response.getCode()); 
 
    }) 
 
; 
 

 

 
/* also appears to do nothing */ 
 
requestify.request('https://www.example.com/api/get.json', { 
 
    method: 'GET' 
 
}) 
 
    .then(function(response) { 
 
    console.log('responsebody', response.getBody()); 
 
    console.log('response headers',response.getHeaders()); 
 
    console.log('responseheader Accept', response.getHeader('Accept')); 
 
    console.log('response code', response.getCode()); 
 
    console.log('responsebody RAW', response.body); 
 
    }) 
 
    .fail(function (response) { 
 
    console.log('response Error', response.getCode()); 
 
    }) 
 
;

+0

我在OP中包含console.log()是错字。我的实际项目在.log()中有不同的消息。我确实需要()。如果这些建议中的任何一个都是这个问题,那么代码不可能无错地编译。 – Steve

0

我不能说,它到底是什么,但requestify不工作一个控制台应用程序。但正在尝试迁移到Web应用程序的第一次尝试。