Categories
JavaScript Answers

How to make a HTTPS POST in Node.js without any third party module?

Sometimes, we want to make a HTTPS POST in Node.js without any third party module.

In this article, we’ll look at how to make a HTTPS POST in Node.js without any third party module.

How to make a HTTPS POST in Node.js without any third party module?

To make a HTTPS POST in Node.js without any third party module, we can use the https.request method.

For instance, we write

const https = require('https');

const postData = JSON.stringify({
  'msg': 'Hello World!'
});

const options = {
  hostname: 'jsonplaceholder.com',
  port: 443,
  path: '/post',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

const req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});

req.write(postData);
req.end();

to call https.request with the options for the request, which includes the request URL, method, and headers.

We get the response from the callback we pass into https.request as the 2nd argument.

And we pass in the request body by calling the req.write.

res.on lets us listen to the response by listening to the 'data' event.

And the 'error' event is emitted when there’s an error.

Conclusion

To make a HTTPS POST in Node.js without any third party module, we can use the https.request method.

Categories
JavaScript Answers

How to fix Node.js dotenv isn’t loading environment variables?

Sometimes, we want to fix Node.js dotenv isn’t loading environment variables.

In this article, we’ll look at how to fix Node.js dotenv isn’t loading environment variables.

How to fix Node.js dotenv isn’t loading environment variables?

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

For instance, we write

const path = require('path')
require('dotenv').config({
  path: path.resolve(__dirname, '../.env')
})

to call config with an object with path set to the path of the .env file.

As a result, the .env file’s environment variables will be loaded into process.env.

Conclusion

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

Categories
JavaScript Answers

How to make a query with OR condition with Node.js Sequelize?

Sometimes, we want to make a query with OR condition with Node.js Sequelize.

In this article, we’ll look at how to make a query with OR condition with Node.js Sequelize.

How to make a query with OR condition with Node.js Sequelize?

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.

For instance, w write

const {
  Op
} = require('Sequelize');

const r = await to(Student.findAll({
  where: {
    LastName: "Doe",
    FirstName: {
      [Op.or]: ["John", "Jane"]
    },
    Age: {
      [Op.between]: [18, 24]
    }
  }
}));

to call findAll with the where property set to an object with the fields we’re filtering by.

And we use the Op.or property set to an array of values to search for entries with FirstName set to 'John' or 'Jane'.

We use Op.between to search for entries with Age between 18 and 24.

Each property in where will be joined together with OR.

Conclusion

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.

Categories
JavaScript Answers

How to run query after populate in Mongoose?

Sometimes, we want to run query after populate in Mongoose.

In this article, we’ll look at how to run query after populate in Mongoose.

How to run query after populate in Mongoose?

To run query after populate in Mongoose, we call populate with an object with the query.

For instance, we write

const query = Models.Item.find({});

query
  .desc('dateCreated')
  .populate('tags', null, {
    tagName: {
      $in: ['funny', 'politics']
    }
  })

to call populate with an object that has the field to query as the first argument and an object with the predicate data as the 3rd argument.

Therefore, we query the tags field to find the items with the tagName equal to 'funny' or 'politics' and populate each entry in the results with the query result from calling populate.

Conclusion

To run query after populate in Mongoose, we call populate with an object with the query.

Categories
JavaScript Answers

How to convert relative path to absolute with Node.js?

Sometimes, we want to convert relative path to absolute with Node.js.

In this article, we’ll look at how to convert relative path to absolute with Node.js.

How to convert relative path to absolute with Node.js?

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

For instance, we write

const {
  resolve
} = require('path')

resolve('../../dir/tmp.txt')

to call resolve with a relative path string to return the absolute path equivalent of the relative path as a string.

Conclusion

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.