Categories
JavaScript Answers React Answers

How to fix React-router URLs don’t work when refreshing or writing manually?

Sometimes, we want to fix React-router URLs don’t work when refreshing or writing manually.

In this article, we’ll look at how to fix React-router URLs don’t work when refreshing or writing manually.

How to fix React-router URLs don’t work when refreshing or writing manually?

To fix React-router URLs don’t work when refreshing or writing manually, we should make sure that we’re redirecting the React app URLs to index.html in our web server.

For instance, in Apache, we add the following to the .htaccess file.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]
</IfModule>

We intercept all URLs with

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

And then we redirect them to index.html with

RewriteRule . /index.html [L]

Conclusion

To fix React-router URLs don’t work when refreshing or writing manually, we should make sure that we’re redirecting the React app URLs to index.html in our web server.

Categories
JavaScript Answers

How to send custom data along with handshake data in socket.io?

Sometimes, we want to send custom data along with handshake data in socket.io.

In this article, we’ll look at how to send custom data along with handshake data in socket.io.

How to send custom data along with handshake data in socket.io?

To send custom data along with handshake data in socket.io, we can call io with an object with the custom data we want to send as the 2nd argument on client side.

For instance, on client side, we write

const socket = io("http://127.0.0.1:3000/", {
  query: "foo=bar"
});

to call io to create the connection.

Then on server side, we write

io.use((socket, next) => {
  console.log(socket.handshake.query);
  if (socket.handshake.query.foo === "bar") {
    return next();
  }
  next(new Error('Authentication error'));
});

to call io.use with a callback to listen for handshakes.

And we get the value of the query property of the object we called io with on client side with socket.handshake.query.

Conclusion

To send custom data along with handshake data in socket.io, we can call io with an object with the custom data we want to send as the 2nd argument on client side.

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.