Node.js Legacy Tunnel Proxy: phantomjs / casperjs / request

Deprecated but still widely deployed Node.js tools integrating 16Yun Crawler Proxy.

16Yun Engineering TeamApr 21, 20261 min read

phantomjs

PhantomJS 已停止维护,但仍有大量旧项目在使用。通过 --proxy--proxy-auth 参数配置:

// main.js — 通过子进程启动 phantomjs
const { spawn } = require('child_process');
const path = require('path');

const host = process.env.PROXY_HOST || 't.16yun.cn';
const port = process.env.PROXY_PORT || '31111';
const user = process.env.PROXY_USERNAME || 'user';
const pass = process.env.PROXY_PASSWORD || 'password';
const target = process.env.TARGET_URL || 'https://httpbin.org/ip';

const child = spawn('phantomjs', [
    `--proxy=${host}:${port}`,
    `--proxy-auth=${user}:${pass}`,
    'http-demo.js', target,
], { stdio: 'inherit' });
// http-demo.js — PhantomJS 页面脚本
var page = require('webpage').create();
var system = require('system');
var target = system.args[1] || 'https://httpbin.org/ip';

page.open(target, function(status) {
    if (status === 'success') {
        console.log(page.plainText);
    }
    phantom.exit();
});

casperjs

CasperJS 基于 PhantomJS,通过 --proxy 参数:

casperjs --proxy=http://t.16yun.cn:31111 \
         --proxy-auth=user:pass \
         main.js
var casper = require('casper').create();
var target = 'https://httpbin.org/ip';

casper.start(target, function() {
    this.echo(this.getPageContent());
});

casper.run();

request

request 库已停止维护(deprecated),但仍有海量存量代码:

const request = require('request');

const host = process.env.PROXY_HOST || 't.16yun.cn';
const port = process.env.PROXY_PORT || '31111';

request({
    url: 'https://httpbin.org/ip',
    proxy: `http://${user}:${pass}@${host}:${port}`,
    headers: { 'Proxy-Tunnel': process.env.PROXY_TUNNEL || 'request-demo' },
    timeout: 15000,
}, (err, resp, body) => {
    if (!err) console.log(body);
});

迁移建议

工具状态推荐替代
PhantomJS2023 年归档,停止维护Puppeteer / Playwright
CasperJS随 PhantomJS 停止Playwright / Puppeteer
request2020 年 deprecatedundici / got / axios

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.