Categories
JavaScript Answers

How to use an http proxy with Node.js http.Client?

Sometimes, we want to use an http proxy with Node.js http.Client.

In this article, we’ll look at how to use an http proxy with Node.js http.Client.

How to use an http proxy with Node.js http.Client?

To use an http proxy with Node.js http.Client, we can use the http.get method to connect to the proxy.

For instance, we write

const http = require("http");

const options = {
  host: "proxy",
  port: 8080,
  path: "http://www.google.com",
  headers: {
    Host: "www.google.com"
  }
};

http.get(options, (res) => {
  console.log(res);
  res.pipe(process.stdout);
});

to call http.get with the options for the connection.

We set the host to the proxy URL.

path has the path that we want to proxy with the proxy.

And headers has the Host with the host URL to proxy.

In the get callback, we get the response from res.

Conclusion

To use an http proxy with Node.js http.Client, we can use the http.get method to connect to the proxy.

Categories
JavaScript Answers

How to fix the ‘db.collection is not a function’ error when using MongoClient v3.0?

Sometimes, we want to fix the ‘db.collection is not a function’ error when using MongoClient v3.0.

In this article, we’ll look at how to fix the ‘db.collection is not a function’ error when using MongoClient v3.0.

How to fix the ‘db.collection is not a function’ error when using MongoClient v3.0?

To fix the ‘db.collection is not a function’ error when using MongoClient v3.0, we should use the client.db method to return the database handle.

For instance, we write

MongoClient.connect('mongodb://localhost', (err, client) => {
  if (err) {
    throw err;
  }

  const db = client.db('mytestingdb');

  db.collection('customers').findOne({}, (findErr, result) => {
    if (findErr) {
      throw findErr;
    }
    console.log(result.name);
    client.close();
  });
});

to call MongoClient.connect to connect to our database.

In the callback we pass into connect, we get the client object, which we use to get the db handle with the client.db method.

Then we call db.collection to query the 'customers' collection.

The query results are in result.

Finally, we call client.close to close the database connection when we’re done.

Conclusion

To fix the ‘db.collection is not a function’ error when using MongoClient v3.0, we should use the client.db method to return the database handle.

Categories
JavaScript Answers

How to use multiple database in a single Node.js project with Mongoose?

Sometimes, we want to use multiple database in a single Node.js project with Mongoose.

In this article, we’ll look at how to use multiple database in a single Node.js project with Mongoose.

How to use multiple database in a single Node.js project with Mongoose?

To use multiple database in a single Node.js project with Mongoose, we can use the connection’s model method.

For instance, we write

const conn = mongoose.createConnection('mongodb://localhost/testA');
const conn2 = mongoose.createConnection('mongodb://localhost/testB');

const ModelA = conn.model('Model', new mongoose.Schema({
  title: {
    type: String,
    default: 'model in testA database'
  }
}));

const ModelB = conn2.model('Model', new mongoose.Schema({
  title: {
    type: String,
    default: 'model in testB database'
  }
}));

to call mongoose.createConnection to connect to 2 databases.

Then we call model on each to create 2 models from 2 different connections and 2 different schemas.

We add the title property to each schema with type String.

Conclusion

To use multiple database in a single Node.js project with Mongoose, we can use the connection’s model method.

Categories
JavaScript Answers Vue Answers

How to use environment variables with Vue.js?

Sometimes, we want to use environment variables with Vue.js.

In this article, we’ll look at how to use environment variables with Vue.js.

How to use environment variables with Vue.js?

To use environment variables with Vue.js, we can add an .env file with the variable keys starting with VUE_APP.

For instance, we write

VUE_APP_MY_ENV_VARIABLE=value
VUE_APP_ANOTHER_VARIABLE=value

Then we can access them in our app with

process.env.VUE_APP_MY_ENV_VARIABLE
process.env.VUE_APP_ANOTHER_VARIABLE

This works if the app is created with Vue CLI.

We can specify .env files with different extensions for different environments.

For instance, we can have

.env                # loaded in all cases
.env.local          # loaded in all cases, ignored by git
.env.[mode]         # only loaded in specified mode
.env.[mode].local   # only loaded in specified mode, ignored by git

Then we can use npm run serve --mode=[mode] to run the Vue project with the given config.

Conclusion

To use environment variables with Vue.js, we can add an .env file with the variable keys starting with VUE_APP.

Categories
JavaScript Answers

How to create a schema from a preexisting collection with Mongoose?

Sometimes, we want to create a schema from a preexisting collection with Mongoose.

In this article, we’ll look at how to create a schema from a preexisting collection with Mongoose.

How to create a schema from a preexisting collection with Mongoose?

To create a schema from a preexisting collection with Mongoose, we can set the collection option when we’re creating the schema.

For instance, we write

new Schema({
  url: String,
  text: String,
  id: Number
}, {
  collection: 'question'
});

to call the Schema constructor with an object with the properties of the documents and another object that specifies the collection` that the schema references.

We reference the question collection with the schema we created.

Conclusion

To create a schema from a preexisting collection with Mongoose, we can set the collection option when we’re creating the schema.