Categories
JavaScript Answers

How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js?

To copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js, we use the coptObject method.

For instance, we write

const AWS = require("aws-sdk");
AWS.config.update({
  credentials: new AWS.Credentials("..."),
});
AWS.config.setPromisesDependency(require("bluebird"));
const s3 = new AWS.S3();

const bucketName = "bucketName";
const folderToMove = "folderToMove/";
const destinationFolder = "destinationFolder/";
try {
  const listObjectsResponse = await s3
    .listObjects({
      Bucket: bucketName,
      Prefix: folderToMove,
      Delimiter: "/",
    })
    .promise();

  const folderContentInfo = listObjectsResponse.Contents;
  const folderPrefix = listObjectsResponse.Prefix;

  await Promise.all(
    folderContentInfo.map(async (fileInfo) => {
      await s3
        .copyObject({
          Bucket: bucketName,
          CopySource: `${bucketName}/${fileInfo.Key}`,
          Key: `${destinationFolder}/${fileInfo.Key.replace(folderPrefix, "")}`,
        })
        .promise();

      await s3
        .deleteObject({
          Bucket: bucketName,
          Key: fileInfo.Key,
        })
        .promise();
    })
  );
} catch (err) {
  console.error(err);
}

to call folderContentInfo.map with a callback to return an array of promises that we get from copyObject.

We call copyObject with an object with the Bucket, the CopySource source path, and the Key to copy to.

Then we call delete object with an object with the Bucket and Key to delete the old object.

We call Promise.all with the array to run all the copy and delete operations.

Categories
JavaScript Answers

How to route all requests to index.html with Node Express?

To route all requests to index.html with Node Express, we call the sendFile method.

For instance, we write

const express = require("express");
const server = express();

server.use("/public", express.static(__dirname + "/static-files-dir"));

server.get("/some-route", (req, res) => {
  res.send("ok");
});

server.get("/*", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

const port = 8000;
server.listen(port, () => {
  console.log("server listening on port " + port);
});

to create a catch route by calling server.get with '.*'.

And then we call sendFile with the path of index.html to render that as the response.

Categories
JavaScript Answers

How to redirect a POST request with Node.js Express?

To redirect a POST request with Node.js Express, we call redirect to redirect.

For instance, we write

app.post("/", (req, res) => {
  res.redirect(301, "/test");
});

to call redirect with the status code and the URL of the direct to do the redirect.

Categories
JavaScript Answers

How to get error status code with Node http get?

To get error status code with Node http get, we can listen for the error event.

For instance, we write

const https = require("https");

const request = https.get("https://example.com", (response) => {
  console.log(response.statusCode);
});

request.on("error", (error) => {
  console.error(error.status);
});

to call get to make a request to https://example.com.

We get the statusCode for a normal response with response.statusCode in the callback.

If there’s an error, then the error event callback is called and we get the status from error.status.

We listen for the error event with request.on`.

Categories
JavaScript Answers

How to create a zero filled JavaScript array?

To create a zero filled JavaScript array, we use the fill method.

For instance, we write

const arr = Array(10).fill(0);

to call Array with 10 to create an array with 10 empty slots.

Then we call fill with 0 to fill all the empty slots with 0.