Docs Home / Node.js Driver

Enable SOCKS5 Proxy Support启用SOCKS5代理支持

Overview概述

In this guide, you can learn how to connect to MongoDB instances by using a SOCKS5 proxy. SOCKS5 is a standardized protocol for connecting to network services through a proxy server.在本指南中,您可以学习如何使用SOCKS5代理连接到MongoDB实例。SOCKS5是用于通过代理服务器连接到网络服务的标准化协议。

Tip

To learn more about the SOCKS5 protocol, see the Wikipedia entry on SOCKS.要了解有关SOCKS5协议的更多信息,请参阅维基百科中关于SOCKS的条目。

Install the socks Package安装袜子包装

Starting in version 6.0 of the Node.js driver, you must install the socks package to use SOCKS5 proxy support in your application. You can install socks by running the following command in your shell:从Node.js驱动程序的6.0版本开始,您必须安装socks包才能在应用程序中使用SOCKS5代理支持。您可以通过在shell中运行以下命令来安装socks

npm i socks

SOCKS5 Client OptionsSOCKS5客户端选项

You can set options in your MongoClientOptions instance or in your connection URI to configure SOCKS5 proxy support for your connection. The following table describes the client options related to SOCKS5:您可以在MongoClientOptions实例或连接URI中设置选项,为您的连接配置SOCKS5代理支持。下表描述了与SOCKS5相关的客户端选项:

Name名称Accepted Values接受值Default Value默认值Description描述
proxyHoststringnullSpecifies the SOCKS5 proxy IPv4 address, IPv6 address, or domain name.指定SOCKS5代理IPv4地址、IPv6地址或域名。
proxyPortnon-negative integernullSpecifies the TCP port number of the SOCKS5 proxy server. If you set the proxyHost option, the value of this option defaults to 1080.指定SOCKS5代理服务器的TCP端口号。如果设置proxyHost选项,则此选项的值默认为1080
proxyUsernamestringnullSpecifies the username for authentication to the SOCKS5 proxy server. If you set this option to a zero-length string, the driver ignores it.指定用于向SOCKS5代理服务器进行身份验证的用户名。如果将此选项设置为零长度字符串,驱动程序将忽略它。
proxyPasswordstringnullSpecifies the password for authentication to the SOCKS5 proxy server. If you set this option to a zero-length string, the driver ignores it.指定用于向SOCKS5代理服务器进行身份验证的密码。如果将此选项设置为零长度字符串,驱动程序将忽略它。

Important

The driver throws an error if you set the proxyPort, proxyUsername, or proxyPassword options without setting the proxyHost option.如果您设置了proxyPortproxyUsernameproxyPassword选项而没有设置proxyHost选项,则驱动程序会抛出错误。

Example示例

This example shows how to instantiate a MongoClient that uses SOCKS5 proxy support. The following example code specifies proxy server options and connects to MongoDB:此示例显示了如何实例化使用SOCKS5代理支持的MongoClient。以下示例代码指定了代理服务器选项并连接到MongoDB:

// Replace the placeholder with your connection string用连接字符串替换占位符
const uri = "<connection string uri>";

// Replace the placeholders with your SOCKS5 proxy server details用您的SOCKS5代理服务器详细信息替换占位符
const socksOptions = {
proxyHost: "<host>",
proxyPort: 1080,
proxyUsername: "<username>",
proxyPassword: "<password>",
};

// Create a new client with the proxy server details使用代理服务器详细信息创建新客户端
const client = new MongoClient(uri, socksOptions);

Tip

The preceding sample code uses placeholders for the connection URI and proxy server details. To run this code, you must replace these placeholders with the information for your deployment and proxy server.前面的示例代码使用占位符作为连接URI和代理服务器详细信息。要运行此代码,您必须用部署和代理服务器的信息替换这些占位符。

Additional Information附加信息

For more information about SOCKS5 proxy support, see the MongoDB SOCKS5 specification.有关SOCKS5代理支持的更多信息,请参阅MongoDB SOCKS5规范

API Documentation文档

To learn more about the methods and types discussed in this guide, see the following API Documentation:要了解有关本指南中讨论的方法和类型的更多信息,请参阅以下API文档: