简体   繁体   中英

How to use Fixie Socks (Heroku add-on) to connect to mongodb via mongoose

I have a node.js express api which I host on heroku. It connects to mongodb atlas via mongoose as follows

mongoose.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PWD}@${process.env.MONGO_HOST}/${process.env.MONGO_DEFAULT_DB}?retryWrites=true`, {
  useNewUrlParser: true,
  autoReconnect: true,
  keepAlive: 300000,
  connectTimeoutMS: 300000,
  socketTimeoutMS: 300000
}
)
.then(result => {
  console.log('Connected and listening to requests!');
  app.listen(process.env.PORT || 3000);
.catch(err => console.log(err));

I want to use Atlas MongoDB Cloud's whitelist for the Heroku app, instead of opening access up to all. Heroku doesn't have fixed a IP address but makes them possible via an add-on called Fixie Socks , which acts as a proxy for outbound traffic.

How can I use this add-on to get the connection to work? The documentation gives several examples on how to connect to other services and databases, but there is no help on mongodb. All examples use the FIXIE_SOCKS_HOST which contains the SOCKSV5 proxy URL, user, pass and port, but I'm at a loss on how to use it in conjunction with the mongoose.connect method.

This question has been asked before , but when a different add-on was used (Fixie vs Fixie Socks), which didn't work.

I tried Fixie Socks but couldn't get it to work. But managed to successfully connect to Mongodb Atlas database with Quota Guard's static IP addresses following this documentation:

https://support.quotaguard.com/support/solutions/articles/12000066411-how-to-connect-to-mongodb-using-quotaguard

The trick is to use the use mongodb atlas' connection string without +srv command. To do that use an older driver version and it will provide you a connection string with 3 replica servers (each with 27017 port). Then create 3 tunnels at the Quota Guard dashboard.

Make sure you download the gqtunnel package and extract it your app's root folder. Then you just have to add the bin/qgtunnel to your procfile.

Example: web: bin/qgtunnel your-application your arguments

I manage to connect from Heroku + node.js + Fixie SOCK add-on to MongoDB Atlas. A few parameters in MongoClient need to be set to direct MongoDB traffic to Fixie proxy. This is the snippet:

const {MongoClient, ServerApiVersion} = require('mongodb'); 

const username = process.env.USERNAME;
const password = process.env.PASSWORD;
const host = process.env.MONGO_DB_HOST;

const uri = "mongodb+srv://" + username + ":" + password + "@" + host + "/?retryWrites=true&w=majority";


const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/@/]+'));

const client = new MongoClient(uri, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true, 
    serverApi: ServerApiVersion.v1,
    proxyUsername: fixieData[0];
    proxyPassword: fixieData[1];
    proxyHost: fixieData[2];
    proxyPort: fixieData[3];
});

Some of the MongoDB client connection options can be found here: https://mongodb.github.io/node-mongodb-native/4.5/interfaces/ConnectionOptions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM