Categories
JavaScript Answers

How to add the sprintf equivalent for Node.js?

Sometimes, we want to add the sprintf equivalent for Node.js.

In this article, we’ll look at how to add the sprintf equivalent for Node.js.

How to add the sprintf equivalent for Node.js?

To add the sprintf equivalent for Node.js, we can use the util.format method.

For instance, we write

util.format('hello %s', 'world');

to call util.format with the format string and the values for the placeholders in the string.

Therefore, we see 'hello world' logged.

How to add the sprintf equivalent for Node.js?

To add the sprintf equivalent for Node.js, we can use the util.format method.

Categories
JavaScript Answers

How to download a file to disk from an AWS S3 bucket with Node.js?

Sometimes, we want to download a file to disk from an AWS S3 bucket with Node.js.

In this article, we’ll look at how to download a file to disk from an AWS S3 bucket with Node.js.

How to download a file to disk from an AWS S3 bucket with Node.js?

To download a file to disk from an AWS S3 bucket with Node.js, we can use the getObject method.

For instance, we write

const s3 = new AWS.S3();

const s3Params = {
  Bucket: 'your bucket',
  Key: 'path/to/the/file.ext'
};

s3.getObject(s3Params, (err, res) => {
  if (err === null) {
    res.attachment('file.ext');
    res.send(data.Body);
  } else {
    res.status(500).send(err);
  }
});

to call getObject with an object with the Bucket and Key to the file.

Then we get the data from res and call res.attachment to write the file data to the path.

And then we call res.send to put the file data into the file.

Conclusion

To download a file to disk from an AWS S3 bucket with Node.js, we can use the getObject method.

Categories
JavaScript Answers

How to write an array to file with Node.js?

Sometimes, we want to write an array to file with Node.js.

In this article, we’ll look at how to write an array to file with Node.js.

How to write an array to file with Node.js?

To write an array to file with Node.js, we can create a write stream.

For instance, we write

const fs = require('fs');

const file = fs.createWriteStream('array.txt');

file.on('error', (err) => {
  /* error handling */
});

arr.forEach((v) => {
  file.write(v.join(', ') + '\n');
});

file.end();

to call fs.createWriteStream with the path of the file to write to.

Then we call arr.forEach to loop through the array and call file.write to write to the stringified version of the array to the the stream.

And once, we’re done, we call file.end to close the stream.

Conclusion

To write an array to file with Node.js, we can create a write stream.

Categories
JavaScript Answers

How to add unit tests for Node.js and Socket.io?

Sometimes, we want to add unit tests for Node.js and Socket.io.

In this article, we’ll look at how to add unit tests for Node.js and Socket.io.

How to add unit tests for Node.js and Socket.io?

To add unit tests for Node.js and Socket.io, we can set up our socket.io connection before each test, and clear the connection after each test.

For instance, we write

const io = require('socket.io-client')
const assert = require('assert')
const expect = require('expect.js');

describe('Suite of unit tests', () => {
  let socket;

  beforeEach((done) => {
    socket = io.connect('http://localhost:3001', {
      'reconnection delay': 0,
      'reopen delay': 0,
      'force new connection': true
    });
    socket.on('connect', () => {
      console.log('worked...');
      done();
    });
    socket.on('disconnect', () => {
      console.log('disconnected...');
    })
  });

  afterEach((done) => {
    if (socket.connected) {
      console.log('disconnecting...');
      socket.disconnect();
    } else {
      console.log('no connection to break...');
    }
    done();
  });

  describe('First test', () => {

    it('Doing some things with indexOf()', (done) => {
      expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
      expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
      done();
    });

    it('Doing something else with indexOf()', (done) => {
      expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
      expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
      done();
    });

  });

});

to call io.connect in the beforeEach callback to connect to our socket.io server before each test.

The we call done in the 'connect' event callback to run our test.

Likewise, we in the afterEach callback, we call socket.disconnect to disconnect from our socket.io server after each test is socket.connected is true.

Then in our tests, we run some test code and then call done when we’re done.

Conclusion

To add unit tests for Node.js and Socket.io, we can set up our socket.io connection before each test, and clear the connection after each test.

Categories
JavaScript Answers

How to make synchronous requests in Node.js?

Sometimes, we want to make synchronous requests in Node.js.

In this article, we’ll look at how to make synchronous requests in Node.js.

How to make synchronous requests in Node.js?

To make synchronous requests in Node.js, we can use the sync-request module.

To install it, we run

npm i sync-request

Then we use it by writing

const request = require('sync-request')

try {
  const res1 = request('GET', url1);
  const res2 = request('GET', url2);
} catch (e) {}

to call request with 'GET' to make GET requests to url1 and url2.

Conclusion

To make synchronous requests in Node.js, we can use the sync-request module.