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

spidex

v3.0.1

Published

A web crawler for node.js, even support hessian request.

Downloads

437

Readme

Spidex

npm version npm downloads Build Status Coverage Status

Spidex is a versatile web requester for Node.js and browsers, designed to simplify HTTP requests with a clean and intuitive API.

Features

  • Supports both Node.js and browser environments.
  • Handles GET, POST, PUT, DELETE and other methods.
  • Customizable request options including headers, timeouts, and charsets.
  • Built-in support for various character encodings.
  • Hessian v2 protocol support (Node.js only).
  • Event-based error handling.

Installation

Install Spidex using npm:

npm install spidex --save

Usage

Basic Request

const spidex = require('spidex');

spidex.get('https://api.example.com/data', (content, statusCode, responseHeaders) => {
  console.log('Response:', content);
  console.log('Status:', statusCode);
  console.log('Headers:', responseHeaders);
}).on('error', err => {
  console.error('Error:', err);
});

Request with Options

spidex.post('https://api.example.com/users', {
  data: JSON.stringify({ username: 'john_doe', email: '[email protected]' }),
  header: { 'Content-Type': 'application/json' },
  charset: 'utf8',
  timeout: 5000
}, (content, statusCode, responseHeaders) => {
  console.log('User created:', content);
}).on('error', (err) => {
  console.error('Error creating user:', err);
});

Supported Basic HTTP Methods

Spidex supports the following HTTP methods:

  • spidex.get(url, [options], [callback])
  • spidex.post(url, [options], [callback])
  • spidex.put(url, [options], [callback])
  • spidex.delete(url, [options], [callback])

Each method returns an EventEmitter that emits an 'error' event if an error occurs.

Supported Other HTTP Methods

  • spidex.method(url, method, [options], [callback])

Request Options

The options object can include the following properties:

  • data: Request body (string, object, or Buffer)
  • header: Custom request headers
  • charset: Character encoding (e.g., 'utf8', 'gbk', 'binary', etc.)
  • timeout: Total request timeout in milliseconds
  • responseTimeout: Response timeout in milliseconds
  • requestTimeout: Request timeout in milliseconds

Parsing Cookies

Spidex provides a utility function to parse cookies from response headers:

const cookies = spidex.parseCookies(responseHeaders);
console.log('Parsed cookies:', cookies);

User Agent Management

You can get or set the default User-Agent string:

// Get the current User-Agent
const currentUA = spidex.getDefaultUserAgent();

// Set a custom User-Agent
spidex.setDefaultUserAgent('MyApp/1.0');

Hessian v2 Support (Node.js only)

Spidex supports Hessian v2 protocol for Node.js environments:

spidex.hessianV2('http://hessian.example.com/api', 'methodName', [arg1, arg2], (err, result) => {
  if (err) {
    console.error('Hessian request failed:', err);
    return;
  }
  console.log('Hessian result:', result);
});

TypeScript Support

Spidex includes TypeScript definitions. You can import and use it in TypeScript projects:

import * as spidex from 'spidex';

spidex.get('https://api.example.com/data', (content, statusCode, responseHeaders) => {
  console.log('Typed response:', content);
});

Error Handling

All Spidex methods return an EventEmitter that emits an 'error' event. You can handle errors by listening to this event:

spidex.get('https://api.example.com/data')
  .on('error', (err) => {
    console.error('Request failed:', err);
  });

License

Spidex is released under the MIT License. See the LICENSE file for details.