Categories
JavaScript Answers

What is the list of Socket.io events?

In this article, we’ll look at the list of Socket.io events.

The list of client side events for socket.io object includes:

  • connect – Fired upon a successful connection.

  • connect_error – Fired upon a connection error. Parameters: Object error object

  • connect_timeout – Fired upon a connection timeout.

  • reconnect. Fired upon a successful reconnection. Parameters: Number – reconnection attempt number

  • reconnect_attempt – Fired upon an attempt to reconnect.

  • reconnecting – Fired upon an attempt to reconnect. Parameters: Number – reconnection attempt number

  • reconnect_error – Fired upon a reconnection attempt error. Parameters: Object error object

  • reconnect_failed – Fired when couldn’t reconnect within reconnectionAttempts

The list of client-side events for socket object includes:

  • connect – Fired upon connecting. error. Fired upon a connection error Parameters: Object – error data

  • disconnect – Fired upon a disconnection.

  • reconnect – Fired upon a successful reconnection. Parameters: Number – reconnection attempt number

  • reconnect_attempt – Fired upon an attempt to reconnect. reconnecting. Fired upon an attempt to reconnect. Parameters: Number – reconnection attempt number

  • reconnect_error – Fired upon a reconnection attempt error. Parameters: Object – error object

  • reconnect_failed – Fired when couldn’t reconnect within reconnectionAttempts

The list of server-side events includes:

connection / connect – Fired upon a connection. Parameters: Socket

  • the incoming socket.
Categories
JavaScript Answers

How to iterate through all files in a bucket with Node.js and Amazon S3?

Sometimes, we want to iterate through all files in a bucket with Node.js and Amazon S3.

In this article, we’ll look at how to iterate through all files in a bucket with Node.js and Amazon S3.

How to iterate through all files in a bucket with Node.js and Amazon S3?

To iterate through all files in a bucket with Node.js and Amazon S3, we can use the listObjectsV2 method.

For instance, we write

const {
  S3
} = require("aws-sdk");
const s3 = new S3();

async function* listAllKeys(opts) {
  const newOpts = {
    ...opts
  }
  do {
    const data = await s3.listObjectsV2(newOpts).promise();
    newOpts.ContinuationToken = data.NextContinuationToken;
    yield data;
  } while (newOpts.ContinuationToken);
}

to add a do-while loop that calls s3.listObjectsV2 with newOpts and then call promise to return a promise with the data from the bucket.

Then we set newOpts.ContinuationToken to data.NextContinuationToken to continue to the next iteration if newOpts.ContinuationToken isn’t null.

We use yield to return the data returned from the promise.

Conclusion

To iterate through all files in a bucket with Node.js and Amazon S3, we can use the listObjectsV2 method.

Categories
JavaScript Answers

How to convert a string to Object Id in Node.js MongoDB native driver?

Sometimes, we want to convert a string to Object Id in Node.js MongoDB native driver.

In this article, we’ll look at how to convert a string to Object Id in Node.js MongoDB native driver.

How to convert a string to Object Id in Node.js MongoDB native driver?

To convert a string to Object Id in Node.js MongoDB native driver, we can use the ObjectId function.

For instance, we write

const {
  ObjectId
} = require('mongodb');

const y = ObjectId("507f191e810c19729de860ea")

to convert the string '507f191e810c19729de860ea' to an MongoDB object ID with ObjectId and assign it to y.

Conclusion

To convert a string to Object Id in Node.js MongoDB native driver, we can use the ObjectId function.

Categories
JavaScript Answers

How to force SSL and HTTPS in Express.js?

Sometimes, we want to force SSL and HTTPS in Express.js.

In this article, we’ll look at how to force SSL and HTTPS in Express.js.

How to force SSL and HTTPS in Express.js?

To force SSL and HTTPS in Express.js, we can call res.redirect to redirect to the HTTPS URL if the request wasn’t made with HTTPS.

For instance, we write

const requireHTTPS = (req, res, next) => {
  if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
    return res.redirect(`https://${req.get('host')}${req.url}`);
  }
  next();
}

to define the requireHTTPS middleware to check if a secure request isn’t made with

!req.secure && req.get('x-forwarded-proto') !== 'https'

And we check if the environment the app is running in isn’t development with

process.env.NODE_ENV !== "development"

If they’re both true, then we call res.redirect to redirect to the HTTPS URL.

Otherwise, we call next to call the next middleware.

Conclusion

To force SSL and HTTPS in Express.js, we can call res.redirect to redirect to the HTTPS URL if the request wasn’t made with HTTPS.

Categories
JavaScript Answers

How to find and update a subdocument with Mongoose?

Sometimes, we want to find and update a subdocument with Mongoose.

In this article, we’ll look at how to find and update a subdocument with Mongoose.

How to find and update a subdocument with Mongoose?

To find and update a subdocument with Mongoose, we call findOneAndUpdate with the $set operator.

For instance, we write

Folder.findOneAndUpdate({
    "_id": folderId,
    "permissions._id": permission._id
  }, {
    "$set": {
      "permissions.$": permission
    }
  },
  (err, doc) => {

  }
);

to call findOneAndUpdate with an object with the $set property.

We use it to set the permissions array to permission by setting $set to

{
  "permissions.$": permission
}

Conclusion

To find and update a subdocument with Mongoose, we call findOneAndUpdate with the $set operator.