SuperAgent

how can superagent and nock work together?

别等时光非礼了梦想. 提交于 2019-12-03 15:37:54
问题 In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly. Here is a simple example where superagent fails to report the mocked data: var agent = require('superagent'); var nock = require('nock'); nock('http://thefabric.com') .get('/testapi.html') .reply(200, {yes: 'it works !'}); agent .get('http://thefabric.com/testapi.html') .end(function(res){ console.log(res.text); }); the res object has no 'text' property. Something

How to post multipart/form-data with node.js superagent

心已入冬 提交于 2019-12-03 07:48:42
问题 I am trying to send the content-type in my superagent post request to multipart/form-data. var myagent = superagent.agent(); myagent .post('http://localhost/endpoint') .set('api_key', apikey) .set('Content-Type', 'multipart/form-data') .send(fields) .end(function(error, response){ if(error) { console.log("Error: " + error); } }); The error I get is: TypeError: Argument must be a string If I remove the: .set('Content-Type', 'multipart/form-data') I don't get any error but my back end is

how can superagent and nock work together?

谁说胖子不能爱 提交于 2019-12-03 06:00:46
In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly. Here is a simple example where superagent fails to report the mocked data: var agent = require('superagent'); var nock = require('nock'); nock('http://thefabric.com') .get('/testapi.html') .reply(200, {yes: 'it works !'}); agent .get('http://thefabric.com/testapi.html') .end(function(res){ console.log(res.text); }); the res object has no 'text' property. Something went wrong. Now if I do the same thing using request: var request = require('request'); var nock =

How can I inject a custom HTTP Header into every request that SuperAgent makes?

血红的双手。 提交于 2019-12-03 05:36:34
Clearly SuperAgent supports custom HTTP headers: request .post('/api/pet') .send({ name: 'Manny', species: 'cat' }) .set('X-API-Key', 'foobar') .set('Accept', 'application/json') .end(function(err, res){ if (res.ok) { alert('yay got ' + JSON.stringify(res.body)); } else { alert('Oh no! error ' + res.text); } }); My Question: If I'm pulling down SuperAgent via npm, how can I inject my own HTTP header across all requests that SuperAgent makes? Note: I'm entire willing to create a new npm package that extends SuperAgent if necessary. I'd just make a separate module with something like this:

Read response output buffer/stream with supertest/superagent on node.js server

核能气质少年 提交于 2019-12-03 02:36:56
I am trying to write a test that checks whether an API route outputs a ZIP file with the correct contents. I am using mocha and supertest for testing, and I would like to actually read the output stream/buffer, read the zip file contents and see if the contents are correct. Any ideas how should I do it? When I try to read res.body , it's just an empty object. request(app) .get( "/api/v1/orders/download?id[]=1&id=2" ) .set( "Authorization", authData ) .expect( 200 ) .expect( 'Content-Type', /application\/zip/ ) .end( function (err, res) { if (err) return done( err ); console.log( 'body:', res

使用node写爬虫入门

匿名 (未验证) 提交于 2019-12-02 23:52:01
最近看了node能做爬虫,所以就试了一下,一下是整个过程的记录 1、新建文件夹baidunews 2、在上边新建的文件夹下输入npm init进行初始化 3、初始化完成后下载需要的依赖包   npm install express   npm install cheerio   npm install superagent 4、在baidunews文件夹下新建index.js文件 5、在文件中加入一下代码    const express = require('express'); const app = express(); // ... let server = app.listen(3000, function () { let host = server.address().address; let port = server.address().port; console.log('Your App is running at http://%s:%s', host, port); }); /** * [description] - 跟路由 */ // 当一个get请求 http://localhost:3000时,就会后面的async函数 app.get('/', async (req, res, next) => { res.send(hotNews); }); //

Nodejs -- 使用koa2搭建数据爬虫

匿名 (未验证) 提交于 2019-12-02 23:26:52
cheerio : 则能够对请求结果进行解析,解析方式和jquery的解析方式几乎完全相同 cheerio中文文档 开发参考node - cheerio模块 superagent : 能够实现主动发起get/post/delete等请求 superagent-charset : 解决爬虫数据中文乱码问题,早期版本单独使用,现配合superagent使用 koa2 : 搭建服务器环境等等 koa-router: koa路由,用于根据路由访问对应代码块,逻辑编写等作用(把他理解为像日常API接口就好) knex : 操作数据库,支持多种数据库,这里使用mysql,需要mysql中间件 开发参考knex笔记 在项目根目录下 npm init 一路回车,初始化项目环境,出现package.json文件,然后执行以下命令安装项目依赖 npm i --save cheerio superagent superagent-charset koa-router koa knex mysql 在项目根目录下创建app.js文件,编写coding const Koa = require('koa'), Router = require('koa-router'), cheerio = require('cheerio'), charset = require('superagent-charset')

How to post multipart/form-data with node.js superagent

别等时光非礼了梦想. 提交于 2019-12-02 20:29:55
I am trying to send the content-type in my superagent post request to multipart/form-data. var myagent = superagent.agent(); myagent .post('http://localhost/endpoint') .set('api_key', apikey) .set('Content-Type', 'multipart/form-data') .send(fields) .end(function(error, response){ if(error) { console.log("Error: " + error); } }); The error I get is: TypeError: Argument must be a string If I remove the: .set('Content-Type', 'multipart/form-data') I don't get any error but my back end is receiving the request as content-type: application/json How can I force the content type to be multipart/form

How to chain http calls with superagent/supertest?

点点圈 提交于 2019-12-02 20:04:45
I am testing an express API with supertest. I couldn't get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test case seem to only execute the last call which is the HTTP GET. it('should respond to GET with added items', function(done) { var agent = request(app); agent.post('/player').type('json').send({name:"Messi"}); agent.post('/player').type('json').send({name:"Maradona"}); agent.get('/player').set("Accept", "application/json") .expect(200) .end(function(err, res) { res.body.should.have.property('items').with.lengthOf(2); done(); }); );

Pipe superagent response to express response

邮差的信 提交于 2019-12-02 04:25:33
问题 I'm trying to "proxy" some file with an express app. Why the code below doesn't work? var app = require('express')() var request = require('superagent') app.get('/image', function(req, res, next) { request('http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge.104.520060.JPG') .then(function(_res) { _res.pipe(res) }) }) app.listen(3001, function() { console.log('listen') }) When I "wget" a file directly it works: $ wget http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge