Categories
JavaScript Answers

How to send Content-Type: application/json post with Node.js?

To send Content-Type: application/json post with Node.js, we call the request function.

For instance, we write

const request = require("request");

const options = {
  uri: "https://www.example.com/urlshortener/v1/url",
  method: "POST",
  json: {
    longUrl: "http://www.google.com/",
  },
};

request(options, (error, response, body) => {
  if (!error && response.statusCode === 200) {
    console.log(body.id);
  }
});

to call request with the options object to make a request.

The method is set to 'POST' to make a post request.

The json property is set to an object with the JSON request body data.

Categories
JavaScript Answers

How to find files by extension, *.html under a folder in Node.js?

To find files by extension, *.html under a folder in Node.js, we use the glob function.

For instance, we write

const glob = require("glob");

glob(__dirname + "/**/*.html", {}, (err, files) => {
  console.log(files);
});

to call glob with the path pattern to search for and a callback that has the files result returned from the search.

Categories
JavaScript Answers

How to get folder path from a file with Node.js?

To get folder path from a file with Node.js, we call the path.dirname method.

For instance, we write

const path = require("path");
const dir = path.dirname("G:\\node-demos\\7-node-module\\demo\\config.json");

to call path.dirname to return the folder path for the "G:\\node-demos\\7-node-module\\demo\\config.json" path.

Categories
JavaScript Answers

How to use Sequelize findAll with sort order in Node.js?

To use Sequelize findAll with sort order in Node.js, we call findAll with an object with the order property.

For instance, we write

const companies = Company.findAll({
  where: {
    id: [46128, 2865, 49569, 1488, 45600, 61991, 1418, 61919, 53326, 61680],
  },
  order: [
    ["id", "DESC"],
    ["name", "ASC"],
  ],
  attributes: ["id", "logo_version", "logo_content_type", "name", "updated_at"],
});

to call findAll with an object with the order property to return the results sorted by id descending and by name ascending.

Categories
JavaScript Answers

How to get response from S3 getObject in Node.js?

To get response from S3 getObject in Node.js, we call the getObject method.

For instance, we write

const AWS = require("aws-sdk");

const s3 = new AWS.S3();

const getObject = async (bucket, objectKey) => {
  try {
    const params = {
      Bucket: bucket,
      Key: objectKey,
    };

    const data = await s3.getObject(params).promise();

    return data.Body.toString("utf-8");
  } catch (e) {
    throw new Error(`Could not retrieve file from S3: ${e.message}`);
  }
};

const myObject = await getObject("my-bucket", "path/to/the/object.txt");

to define the getObject function.

In it we call getObject with the params object that has the Bucket and Key into to get the object.

We call promise to return a promise with the object data.

Then we convert it to a string with data.Body.toString`.