npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requester

v0.1.22

Published

swiss army knife for requests

Downloads

148

Readme

node-requester Build Status

A simple network request helper that is geared towards crawling. (a few keywords GZIP, XML, JSON, PROXIES)

installation

$ npm install requester

super simple to use

var Requester = require('requester'),
	requester = new Requester({debug: 1});

requester.get(/* URL */, function (body) {
	console.log(this.statusCode, body);
});

requester.get(/* URL */, /* REQUEST_OBJECT */, function (body) {
	console.log(this.statusCode, body);
});

you can even use this simple request translation tool

initialization

var Requester = ('requester');

var requester = new Requester({
	cookiejar: true, // basic cookie support, currently doesnt care about domain or path rules
	cookies: {},
	headers: {},
	timeout: 4000,
	retries: 3,
	encoding 'utf8',
	// didRequestFail: null, (this has its own section)
	// signRequest: null, (this has its own section)
	// processResponse: null, (this has its own section)
	dataType: 'RAW' // JSON or XML,
	auth: {username: 'username', password: 'password'}, // basic auth for all requests
	proxies: [{ip: '127.0.0.1', port: 1337}, {ip: '127.0.0.2', port: 1337}, {ip: '127.0.0.3', port: 1337}] // rotating proxy array
});

if you initialize the request object with any of the above properties every request will default to those settings, you can over ride them on a per request basis

var options = {
	encoding: 'binary',
	proxy: {ip: '127.0.0.1', port: 1337},
	data: {foo: 'bar'},
	cookies: {foo: 'bar'},
	auth: {username: 'username', password: 'password'} // basic auth for request
};

requester.get(/* URL */, options, function (body) {
	console.log(body)
});

request objects

they support the following properties

  • {Object} data
  • {String} dataType
  • {Object} headers
  • {Object} cookies
  • {Object} proxy
  • {Object} auth
  • {String} encoding
  • {Number} timeout
  • {Number} retries
  • {Function} didRequestFail
  • {Function} signRequest
  • {Function} processResponse
  • {Boolean} follow
  • {Number} followMax
  • {Number} debug

debugging

you can set debug to the following

  • 1 - outgoing requests
  • 2 - outgoing requests and responses with headers
  • 3 - outgoing requests, responses with headers, and response body

methods

  • get
  • post
  • multipart
  • put
  • del

proxies

request objects support proxies but you also can add / remove them from the proxy rotation like this

var requester = new Requester({
	proxies: [{ip: 127.0.0.1, port: 1337}]
});

requester.addProxies({ip: 127.0.0.1, port: 1337}, {ip: 127.0.0.2, port: 1337}, {ip: 127.0.0.1, port: 1337, auth: {username: 'foo', password: 'bar'}});

requester.removeProxies({ip: 127.0.0.1, port: 1337});

this allows you to do custom checking outside of requester to maintain the proxy list

request response checking

this is a method that gets ran before the actual response callback gets run to ensure that the content is what you're expecting, for example if the content rotates and you're looking for something special you can do

requester.get(
	/ * URL */,
	{
		didRequestFail: function (data) {
			return !data.match(/something/);
		},
		retries: 10
	},
	function (data) {
		console.log(data);
	}
);

this would request the url until it matches the string 'something' in the response (up to 10 attempts)

response preprocessing

lets say the server responds back with invalid JSON

var json = {foo: 'bar'}

you can use a processResponse function to clean it up like this

requester.get(
	/* URL */,
	{
		dataType: 'JSON',
		processResponse: function (body) {
			return body.replace(/^var json = /, '');
		}
	},
	function (body) {
		console.log(body);
	}
);

this is really useful if you want to not repeat response cleanup code

request signatures

you can create a custom request signature function like this

var qs = require('querystring');

var requester = new Requester({
	signRequest: function (data) {
		// do something with the data
		return qs.stringify(data);
	}
});

posting

requester.post(/* URL */, {data: {foo: 'bar', bar: 'foo'}}, function (body) {
	console.log(body)
});

multipart

the multipart request works a little different, in the data object you can prefix a values key with '@' like this

requester.multipart(/* URL */, {data: {'@file': /* FILEPATH */, '@file2': /* FILEPATH */, bar: 'foo'}}, function (body) {
	console.log(body)
});

this will create a multipart request and upload files