Tag: 请求

github说不能parsingjson,而在rest api中创build一个repo

我正在学习GitHub的REST api,并试图通过运行JS创build一个新的存储库。 这里是我创build一个新的回购的function:令牌生成,并且所有访问/作用域被授予我的令牌。 function createNewRepo(userId, name){ var options = { url: urlRoot + "/user/repos", method: 'POST', headers: { "User-Agent": "EnableIssues", "content-type": "application/json", "Authorization": token, "name": name, "description": "This is repo creating by REST", "homepage": "https://github.com", "private": false, "has_issues": true, "has_projects": true, "has_wiki": true, } }; //console.log(eval(options)); request(options, function (error, response, body) { var obj = […]

节点请求返回ECONNREFUSED

我是新来的nodejs和expression。 我正在使用“请求”库来阅读google.com的内容。 我不断收到拒绝连接错误。 我从https://github.com/request/request获取了代码 这是我的first_request.js文件: var request = require('request'); request('http://www.google.com', function (error, response, body) { console.log('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. }); 运行,我到了文件夹,并做node first_request.js 它给我错误: 我究竟做错了什么? 任何指针?

node.js POST请求失败

我试图用node.js执行一个POST请求,但似乎总是超时。 我也尝试使用cURL在PHP中的请求,只是为了确保工作正常。 另外,在本地服务器(127.0.0.1)而不是远程服务器上执行完全相同的请求时,它也可以很好地工作。 Node.js的: var postRequest = { host: "www.facepunch.com", path: "/newreply.php?do=postreply&t=" + threadid, port: 80, method: "POST", headers: { Cookie: "cookie", 'Content-Type': 'application/x-www-form-urlencoded' } }; buffer = ""; var req = http.request( postRequest, function( res ) { console.log( res ); res.on( "data", function( data ) { buffer = buffer + data; } ); res.on( […]

Node.js我的请求没有被阻止

我是新来的node.js,我试图调用一个服务,parsing它的数据,并返回它作为一个视图的一部分。 我似乎无法得到阻止的请求,直到响应完成。 控制台总是在“正确”之前logging“错误”(返回1,2,3数组)。 我错过了什么? app.js var reading = require('./reading'); app.get('/reading', function(req, res){ res.render('reading/index.stache', { locals : { ids : reading.list}, partials : { list : '{{#ids}}{{.}}<br />{{/ids}}' } }); }); reading.js var request, http = require('http'), host = 'google.com', path ='/'; var list = function(){ var connection = http.createClient(80, host), request = connection.request(path); request.addListener('response', function(response){ […]

用Node.js请求库保存图像文件会导致exception

这是从请求的 Github回购 – https://github.com/mikeal/request 您可以将任何响应stream式传输到文件stream。 request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png')) 我已经修改它像这样,其中largeImage是从第三方网站的绝对path。 request(largeImage).pipe(fs.createWriteStream('./img/large/demo.jpg')); 但是我得到以下错误(不知道为什么?)。 我已经看了createWriteStream API,它似乎是正确的。 如果我只包含“demo.jpg”,它工作正常。 但是如果我在“demo.jpg”旁边包含path,则会引发exception。 stream.js:81 throw er; // Unhandled stream error in pipe. ^ Error: ENOENT, open './img/large/demo.jpg' 10 Oct 16:51:02 – [nodemon] app crashed – waiting for file changes before starting… 更新:问题是我没有在/ img /之前包含./public/文件夹。 此更新的代码正常工作: request(largeImage).pipe(fs.createWriteStream('./public/img/large/demo.jpg'));

node.js服务器不接收所有的二进制数据(或客户端不发送所有的二进制数据)

我对node.js非常陌生,并且遇到了一个可能非常基本的问题,我相信我只是没有“获取”某些东西,但是我们继续: 我有两个节目… server.js是一个简单的express / node.js服务器,它接受二进制数据的POST client.js是一个简单的request / node.js客户端,它将大文件的内容传输到服务器 我一直在淘洗stackoverflow的build议,我想我有一个小问题的例子: 客户: var fs = require('fs'); var request = require('request'); fs.createReadStream('test2.zip').pipe(request.post('http://localhost:3000/')); 服务器: var express = require('express'); var app = express(); app.post('/', function(req, res){ var size = 0; req.on('data', function (data) { size += data.length; console.log('Got chunk: ' + data.length + ' total: ' + size); }); req.on('end', […]

ENAMETOOLONG请求xml文件

我正在尝试通过node的请求模块获取我的RSS数据源(xml文件),如下所示: var fs = require('fs') , request = require('request') , feed = 'http://www.benchmark.pl/rss/aktualnosci-pliki.xml'; request.get(feed, function(error, response, body) { if (!error && response.statusCode == 200) { var csv = body; fs.createReadStream(body) .on('error', function (error) { console.error(error); }); } }); 但我得到的错误: { [Error: ENAMETOOLONG, open '<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"> <channel> <title> …. </rss> 在这种情况下我该怎么办?

返回使用nodejs抓取网页的url

我正在尝试构build一个简单的Web应用程序,使用nodejs及其2个模块请求和cheerio来抓取网站。 我设法用下面的代码来完成它: var printURL=function(url){ request(url, (function() { return function(err, resp, body) { if (err) throw err; $ = cheerio.load(body); $('img').each(function(){ console.log($(this).attr('src')); }); } } )()); }; 它在网站上打印图片的URL是正常的,但我真正想要做的是创build一个我可以在函数外部使用的url列表。 我试过这样,但它返回一个空的列表: var urlList=[]; var printURL=function(url){ request(url, (function() { return function(err, resp, body) { if (err) throw err; $ = cheerio.load(body); $('img').each(function(){ urlList.push($(this).attr('src')); }); } } )()); }; 我怎样才能解决这个问题? […]

在导入到JSON格式的MongoDB之前,清理HTTP响应头中的非法字符

在使用Node.js请求HTTP响应并将其导入到MongoDB时,我注意到一个或两个URL将包含非法字符的标题(因为它们正在使用键),当我尝试导入到MongoDB时,这将导致整个脚本崩溃。 下面是一个例子: { "url": "divensurf.com", "statusCode": 200, "headers": { "x-varnish": "2236710953 2236710300", "vary": "Accept-Encoding,Cookie,X-UA-Device", "cache-control": "max-age=7200, must-revalidate", "x-cache": "V1HIT 2", "content-type": "text/html; charset=UTF-8", "page.ly": "v4.0", "x-pingback": "http://divensurf.com/xmlrpc.php", "date": "Thu, 21 Mar 2013 19:40:59 GMT", "transfer-encoding": "chunked", "via": "1.1 varnish", "connection": "keep-alive", "last-modified": "Thu, 21 Mar 2013 19:40:57 GMT", "age": "2" } } 标题/键"page.ly"会使脚本崩溃,因为它包含非法字符. 。 在将这个文档导入到MongoDB之前,有没有什么方法可以通过删除这些非法字符来清除引用中包含的这个键/头? […]

在Node.js中从URL中检索JSON的最好方法是什么?

所以我一直在使用sailsjs从外部网站请求json数据,然后将这些数据发布到创buildpath。 当我第一次运行它,它将工作大约10-12次,然后应用程序将崩溃与event.js抛出呃; 连接ETIMEDOUT 寻找更好的方法来请求https://cex.io/api/ticker/GHS/BTC中的 json数据。 所以我使用sailsjs,并在文件config / bootstrap.js中添加了我的服务来运行。 module.exports.bootstrap = function (cb) { // My tickerService.ticker(); // Runs the app cb(); }; 这是我的一个尝试〜file api / services / tickerservice.js function storeTicker(){ console.log('Running!'); //retrieves info from https://cex.io/api/ticker/GHS/BTC require("cexapi").ticker('GHS/BTC', function(param){ console.log(param); Tickerchart.create( param, function tickerchartCreated (err, tickerchart) {}); }); } module.exports.ticker = function(){ setInterval(storeTicker, 6000); }; Cex.io库Github https://github.com/matveyco/cex.io-api-node.js/blob/master/cexapi.js