Categories
JavaScript Answers

How to read all text from stdin to a string with Node.js?

Sometimes, we want to read all text from stdin to a string with Node.js.

In this article, we’ll look at how to read all text from stdin to a string with Node.js.

How to read all text from stdin to a string with Node.js?

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.

For instance, we write

const fs = require('fs');
const data = fs.readFileSync(0, 'utf-8');

to call readFileSync with 0 to return the text from stdin as a 'utf-8' string.

Conclusion

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.

Categories
JavaScript Answers

How to create or update with Node.js Sequelize?

Sometimes, we want to create or update with Node.js Sequelize.

In this article, we’ll look at how to create or update with Node.js Sequelize.

How to create or update with Node.js Sequelize?

To create or update with Node.js Sequelize, we can use the findOne method to check if the object exists before we update it.

For instance, we write

const upsert = async (values, condition) => {
  const obj = await Model
    .findOne({
      where: condition
    })
  if (obj) {
    return obj.update(values);
  }
  return Model.create(values);
}

to call findOne with an object with the condition we’re looking for.

Then if obj exists, we call obj.update to update the object with the values.

Otherwise, we call create with values to create the object with the values.

Then we call upsert in an async function like

await upsert({
  first_name: 'jane'
}, {
  id: 1234
})

Conclusion

To create or update with Node.js Sequelize, we can use the findOne method to check if the object exists before we update it.

Categories
JavaScript Answers

How to get URL parameters with Node.js and Express?

Sometimes, we want to get URL parameters with Node.js and Express.

In this article, we’ll look at how to get URL parameters with Node.js and Express.

How to get URL parameters with Node.js and Express?

To get URL parameters with Node.js and Express, we can use the req.params property.

For instance, we write

app.get('/documents/:format/:type', (req, res) => {
  const {
    format,
    type
  } = req.params
  //...
});

to get the format and type properties from the req.params object to get the value of the format and type URL parameters.

Conclusion

To get URL parameters with Node.js and Express, we can use the req.params property.

Categories
JavaScript Answers

How to upload a binary file to S3 using AWS SDK for Node.js?

Sometimes, we want to upload a binary file to S3 using AWS SDK for Node.js.

In this article, we’ll look at how to upload a binary file to S3 using AWS SDK for Node.js.

How to upload a binary file to S3 using AWS SDK for Node.js?

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.

For instance, we write

const fileStream = fs.createReadStream("F:/directory/fileName.ext");
const putParams = {
  Bucket: s3bucket,
  Key: s3key,
  Body: fileStream
};

s3.putObject(putParams, (putErr, putData) => {
  if (putErr) {
    console.error(putErr);
  } else {
    console.log(putData);
  }
});

to call putObject with the putParams object.

We set putParams.Body to fileStream to upload its content to S3 at the location set by the Bucket and Key.

Conclusion

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.

Categories
JavaScript Answers

How to loop through dynamic test cases with Jest?

Sometimes, we want to loop through dynamic test cases with Jest.

In this article, we’ll look at how to loop through dynamic test cases with Jest.

How to loop through dynamic test cases with Jest?

To loop through dynamic test cases with Jest, we can use the test.each method.

For instance, we write

test.each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3]
])(
  '.add(%i, %i)',
  (a, b, expected) => {
    expect(a + b).toBe(expected);
  },
);

to call test.each with a nested array of values.

Then we call the returned function with a test name template string and the test callback.

a, b and expected are the entries from the nested array entry of the array we pass into each.

And then we can use the parameters for the tests.

Conclusion

To loop through dynamic test cases with Jest, we can use the test.each method.