Skip to content

Commit

Permalink
Merge pull request #12811 from Automattic/replaceLocalhost
Browse files Browse the repository at this point in the history
chore: replace many occurrences of "localhost" with "127.0.0.1"
  • Loading branch information
vkarpov15 committed Dec 19, 2022
2 parents ce672d2 + 694d914 commit 9d7bf10
Show file tree
Hide file tree
Showing 49 changed files with 121 additions and 121 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -58,7 +58,7 @@ If you'd like to preview your documentation changes, first commit your changes t
* `npm install`
* `npm run docs:view`

Visit `http://localhost:8089` and you should see the docs with your local changes. Make sure you `npm run docs:clean` before committing, because automated generated files to `docs/*` should **not** be in PRs.
Visit `http://127.0.0.1:8089` and you should see the docs with your local changes. Make sure you `npm run docs:clean` before committing, because automated generated files to `docs/*` should **not** be in PRs.

#### Documentation Style Guidelines

Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -66,7 +66,7 @@ const require = createRequire(import.meta.url);

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test')
mongoose.connect('mongodb://127.0.0.1:27017/test')
.then(() => console.log('Connected!'));
```
Expand All @@ -91,12 +91,12 @@ First, we need to define a connection. If your app uses only one database, you s
Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`.

```js
await mongoose.connect('mongodb://localhost/my_database');
await mongoose.connect('mongodb://127.0.0.1/my_database');
```

Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`.
**Note:** _If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed._
**Note:** _If the local connection fails then try using 127.0.0.1 instead of 127.0.0.1. Sometimes issues may arise when the local hostname has been changed._
**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchjs/delete.js
Expand Up @@ -17,11 +17,11 @@ const mongoClient = require('mongodb').MongoClient;
* These are all the benchmark tests for deleting data
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
mongoClient.connect('mongodb://localhost', function (err, client) {
mongoClient.connect('mongodb://127.0.0.1', function (err, client) {
if (err) {
throw err;
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchjs/insert.js
Expand Up @@ -19,12 +19,12 @@ const ObjectId = Schema.Types.ObjectId;
* These are all the benchmark tests for inserting data
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
mongoClient.connect(
'mongodb://localhost/mongoose-bench',
'mongodb://127.0.0.1/mongoose-bench',
function (err, client) {
if (err) {
throw err;
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchjs/multiop.js
Expand Up @@ -19,11 +19,11 @@ const utils = require('../../lib/utils.js');
* These are all the benchmark tests for mixed data operations
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
mongoClient.connect('mongodb://localhost', function (err, client) {
mongoClient.connect('mongodb://127.0.0.1', function (err, client) {
if (err) {
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchjs/population.js
Expand Up @@ -18,7 +18,7 @@ const utils = require('../../lib/utils.js');
* These are all the benchmark tests for population ops
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchjs/read.js
Expand Up @@ -20,11 +20,11 @@ const utils = require('../../lib/utils.js');
* These are all the benchmark tests for reading data
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
mongoClient.connect('mongodb://localhost', function (err, client) {
mongoClient.connect('mongodb://127.0.0.1', function (err, client) {
if (err) {
throw err;
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchjs/update.js
Expand Up @@ -20,11 +20,11 @@ const utils = require('../../lib/utils.js');
* These are all the benchmark tests for updating data
*/

mongoose.connect('mongodb://localhost/mongoose-bench', function (err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) {
if (err) {
throw err;
}
mongoClient.connect('mongodb://localhost', function (err, client) {
mongoClient.connect('mongodb://127.0.0.1', function (err, client) {
if (err) {
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/mem.js
Expand Up @@ -4,7 +4,7 @@ const mongoose = require('../');

const Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/mongoose-bench');
mongoose.connect('mongodb://127.0.0.1/mongoose-bench');

const DocSchema = new Schema({
title: String
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/populate.js
Expand Up @@ -18,7 +18,7 @@ const B = mongoose.model('B', Schema({
let start;
let count = 0;

mongoose.connect('mongodb://localhost/mongoose-bench', function(err) {
mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function(err) {
if (err) {
return done(err);
}
Expand Down
16 changes: 8 additions & 8 deletions docs/connections.md
Expand Up @@ -3,7 +3,7 @@
You can connect to MongoDB with the `mongoose.connect()` method.

```javascript
mongoose.connect('mongodb://localhost:27017/myapp');
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
```

This is the minimum needed to connect the `myapp` database running locally
Expand Down Expand Up @@ -39,7 +39,7 @@ Mongoose lets you start using your models immediately, without waiting for
mongoose to establish a connection to MongoDB.

```javascript
mongoose.connect('mongodb://localhost:27017/myapp');
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
const MyModel = mongoose.model('Test', new Schema({ name: String }));
// Works
MyModel.findOne(function(error, result) { /* ... */ });
Expand All @@ -56,7 +56,7 @@ const MyModel = mongoose.model('Test', new Schema({ name: String }));
MyModel.findOne(function(error, result) { /* ... */ });

setTimeout(function() {
mongoose.connect('mongodb://localhost:27017/myapp');
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
}, 60000);
```

Expand Down Expand Up @@ -101,12 +101,12 @@ There are two classes of errors that can occur with a Mongoose connection.
To handle initial connection errors, you should use `.catch()` or `try/catch` with async/await.

```javascript
mongoose.connect('mongodb://localhost:27017/test').
mongoose.connect('mongodb://127.0.0.1:27017/test').
catch(error => handleError(error));

// Or:
try {
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connect('mongodb://127.0.0.1:27017/test');
} catch (error) {
handleError(error);
}
Expand Down Expand Up @@ -201,9 +201,9 @@ driver. You **can't** set Mongoose-specific options like `bufferCommands`
in the query string.

```javascript
mongoose.connect('mongodb://localhost:27017/test?connectTimeoutMS=1000&bufferCommands=false&authSource=otherdb');
mongoose.connect('mongodb://127.0.0.1:27017/test?connectTimeoutMS=1000&bufferCommands=false&authSource=otherdb');
// The above is equivalent to:
mongoose.connect('mongodb://localhost:27017/test', {
mongoose.connect('mongodb://127.0.0.1:27017/test', {
connectTimeoutMS: 1000
// Note that mongoose will **not** pull `bufferCommands` from the query string
});
Expand Down Expand Up @@ -453,7 +453,7 @@ using your connection options:
mongoose.createConnection(uri, { maxPoolSize: 10 });

// With connection string options
const uri = 'mongodb://localhost:27017/test?maxPoolSize=10';
const uri = 'mongodb://127.0.0.1:27017/test?maxPoolSize=10';
mongoose.createConnection(uri);
```

Expand Down
4 changes: 2 additions & 2 deletions docs/guide.md
Expand Up @@ -274,9 +274,9 @@ While nice for development, it is recommended this behavior be disabled in produ
Disable the behavior by setting the `autoIndex` option of your schema to `false`, or globally on the connection by setting the option `autoIndex` to `false`.

```javascript
mongoose.connect('mongodb://user:pass@localhost:port/database', { autoIndex: false });
mongoose.connect('mongodb://user:pass@127.0.0.1:port/database', { autoIndex: false });
// or
mongoose.createConnection('mongodb://user:pass@localhost:port/database', { autoIndex: false });
mongoose.createConnection('mongodb://user:pass@127.0.0.1:port/database', { autoIndex: false });
// or
mongoose.set('autoIndex', false);
// or
Expand Down
8 changes: 4 additions & 4 deletions docs/migrating_to_5.md
Expand Up @@ -70,10 +70,10 @@ MyModel.find().exec(function() {
// Worked in mongoose 4. Does **not** work in mongoose 5, `mongoose.connect()`
// now returns a promise consistently. This is to avoid the horrible things
// we've done to allow mongoose to be a thenable that resolves to itself.
mongoose.connect('mongodb://localhost:27017/test').model('Test', new Schema({}));
mongoose.connect('mongodb://127.0.0.1:27017/test').model('Test', new Schema({}));

// Do this instead
mongoose.connect('mongodb://localhost:27017/test');
mongoose.connect('mongodb://127.0.0.1:27017/test');
mongoose.model('Test', new Schema({}));
```

Expand All @@ -87,8 +87,8 @@ no longer supports several function signatures for `mongoose.connect()` that
worked in Mongoose 4.x if the `useMongoClient` option was off. Below are some
examples of `mongoose.connect()` calls that do **not** work in Mongoose 5.x.

* `mongoose.connect('localhost', 27017);`
* `mongoose.connect('localhost', 'mydb', 27017);`
* `mongoose.connect('127.0.0.1', 27017);`
* `mongoose.connect('127.0.0.1', 'mydb', 27017);`
* `mongoose.connect('mongodb://host1:27017,mongodb://host2:27017');`

In Mongoose 5.x, the first parameter to `mongoose.connect()` and `mongoose.createConnection()`, if specified, **must** be a [MongoDB connection string](https://docs.mongodb.com/manual/reference/connection-string/). The
Expand Down
2 changes: 1 addition & 1 deletion docs/migrating_to_6.md
Expand Up @@ -90,7 +90,7 @@ res.deletedCount; // Number of documents that were deleted. Replaces `res.n`
// No longer necessary:
mongoose.set('useFindAndModify', false);

await mongoose.connect('mongodb://localhost:27017/test', {
await mongoose.connect('mongodb://127.0.0.1:27017/test', {
useNewUrlParser: true, // <-- no longer necessary
useUnifiedTopology: true // <-- no longer necessary
});
Expand Down
4 changes: 2 additions & 2 deletions docs/models.md
Expand Up @@ -64,13 +64,13 @@ uses is open. Every model has an associated connection. When you use
`mongoose.model()`, your model will use the default mongoose connection.

```javascript
mongoose.connect('mongodb://localhost/gettingstarted');
mongoose.connect('mongodb://127.0.0.1/gettingstarted');
```

If you create a custom connection, use that connection's `model()` function
instead.
```javascript
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const connection = mongoose.createConnection('mongodb://127.0.0.1:27017/test');
const Tank = connection.model('Tank', yourSchema);
```

Expand Down
4 changes: 2 additions & 2 deletions docs/populate.md
Expand Up @@ -444,8 +444,8 @@ Let's say you have a schema representing events, and a schema representing
conversations. Each event has a corresponding conversation thread.

```javascript
const db1 = mongoose.createConnection('mongodb://localhost:27000/db1');
const db2 = mongoose.createConnection('mongodb://localhost:27001/db2');
const db1 = mongoose.createConnection('mongodb://127.0.0.1:27000/db1');
const db2 = mongoose.createConnection('mongodb://127.0.0.1:27001/db2');

const conversationSchema = new Schema({ numMessages: Number });
const Conversation = db2.model('Conversation', conversationSchema);
Expand Down
14 changes: 7 additions & 7 deletions docs/tutorials/ssl.md
Expand Up @@ -3,10 +3,10 @@
Mongoose supports connecting to [MongoDB clusters that require SSL connections](https://docs.mongodb.com/manual/tutorial/configure-ssl/). Setting the `ssl` option to `true` in [`mongoose.connect()`](../api/mongoose.html#mongoose_Mongoose-connect) or your connection string is enough to connect to a MongoDB cluster using SSL:

```javascript
mongoose.connect('mongodb://localhost:27017/test', { ssl: true });
mongoose.connect('mongodb://127.0.0.1:27017/test', { ssl: true });

// Equivalent:
mongoose.connect('mongodb://localhost:27017/test?ssl=true');
mongoose.connect('mongodb://127.0.0.1:27017/test?ssl=true');
```

The `ssl` option defaults to `false` for connection strings that start with `mongodb://`. However,
Expand All @@ -27,7 +27,7 @@ By default, Mongoose validates the SSL certificate against a [certificate author
to `false`.

```javascript
mongoose.connect('mongodb://localhost:27017/test', {
mongoose.connect('mongodb://127.0.0.1:27017/test', {
ssl: true,
sslValidate: false
});
Expand All @@ -49,7 +49,7 @@ server is not registered with an established certificate authority. The solution
[essentially sets a list of allowed SSL certificates](https://mongodb.github.io/node-mongodb-native/2.1/tutorials/connect/ssl/).

```javascript
await mongoose.connect('mongodb://localhost:27017/test', {
await mongoose.connect('mongodb://127.0.0.1:27017/test', {
ssl: true,
sslValidate: true,
// For example, see https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89
Expand All @@ -76,19 +76,19 @@ If you're using [X509 authentication](https://www.mongodb.com/docs/drivers/node/
```javascript
// Do this:
const username = 'myusername';
await mongoose.connect(`mongodb://${encodeURIComponent(username)}@localhost:27017/test`, {
await mongoose.connect(`mongodb://${encodeURIComponent(username)}@127.0.0.1:27017/test`, {
ssl: true,
sslValidate: true,
sslCA: `${__dirname}/rootCA.pem`,
authMechanism: 'MONGODB-X509'
});

// Not this:
await mongoose.connect(`mongodb://localhost:27017/test`, {
await mongoose.connect(`mongodb://127.0.0.1:27017/test`, {
ssl: true,
sslValidate: true,
sslCA: `${__dirname}/rootCA.pem`,
authMechanism: 'MONGODB-X509'.
auth: { username }
});
```
```
2 changes: 1 addition & 1 deletion docs/typescript.md
Expand Up @@ -37,7 +37,7 @@ run().catch(err => console.log(err));

async function run() {
// 4. Connect to MongoDB
await connect('mongodb://localhost:27017/test');
await connect('mongodb://127.0.0.1:27017/test');

const user = new User({
name: 'Bill',
Expand Down
4 changes: 2 additions & 2 deletions docs/typescript/query-helpers.md
Expand Up @@ -69,7 +69,7 @@ const ProjectModel = model<Project, ProjectModelType>('Project', schema);
run().catch(err => console.log(err));

async function run(): Promise<void> {
await connect('mongodb://localhost:27017/test');
await connect('mongodb://127.0.0.1:27017/test');

// Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
await ProjectModel.find().where('stars').gt(1000).byName('mongoose');
Expand Down Expand Up @@ -99,4 +99,4 @@ const ProjectModel = model('Project', ProjectSchema);

// Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
await ProjectModel.find().where('stars').gt(1000).byName('mongoose');
```
```
2 changes: 1 addition & 1 deletion examples/aggregate/aggregate.js
Expand Up @@ -51,7 +51,7 @@ const data = [
];


mongoose.connect('mongodb://localhost/persons', function(err) {
mongoose.connect('mongodb://127.0.0.1/persons', function(err) {
if (err) throw err;

// create all of the dummy people
Expand Down
4 changes: 2 additions & 2 deletions examples/doc-methods.js
Expand Up @@ -36,12 +36,12 @@ CharacterSchema.methods.attack = function() {
const Character = mongoose.model('Character', CharacterSchema);

/**
* Connect to the database on localhost with
* Connect to the database on 127.0.0.1 with
* the default port (27017)
*/

const dbname = 'mongoose-example-doc-methods-' + ((Math.random() * 10000) | 0);
const uri = 'mongodb://localhost/' + dbname;
const uri = 'mongodb://127.0.0.1/' + dbname;

console.log('connecting to %s', uri);

Expand Down
2 changes: 1 addition & 1 deletion examples/express/connection-sharing/README.md
Expand Up @@ -3,4 +3,4 @@ To run:

- Execute `npm install` from this directory
- Execute `node app.js`
- Navigate to `localhost:8000`
- Navigate to `127.0.0.1:8000`
4 changes: 2 additions & 2 deletions examples/express/connection-sharing/app.js
Expand Up @@ -2,7 +2,7 @@
const express = require('express');
const mongoose = require('../../../lib');

const uri = 'mongodb://localhost/mongoose-shared-connection';
const uri = 'mongodb://127.0.0.1/mongoose-shared-connection';
global.db = mongoose.createConnection(uri);

const routes = require('./routes');
Expand All @@ -12,4 +12,4 @@ app.get('/', routes.home);
app.get('/insert', routes.insert);
app.get('/name', routes.modelName);

app.listen(8000, () => console.log('listening on http://localhost:8000'));
app.listen(8000, () => console.log('listening on http://127.0.0.1:8000'));
2 changes: 1 addition & 1 deletion examples/geospatial/geoJSONexample.js
Expand Up @@ -21,7 +21,7 @@ const data = [
];


mongoose.connect('mongodb://localhost/locations', function(err) {
mongoose.connect('mongodb://127.0.0.1/locations', function(err) {
if (err) {
throw err;
}
Expand Down

0 comments on commit 9d7bf10

Please sign in to comment.