Categories
JavaScript Answers

How to ungzip (decompress) a Node.js request’s module gzip response body?

To ungzip (decompress) a Node.js request’s module gzip response body, we use the zlib library.

For instance, we write

const request = require("request");
const zlib = require("zlib");
const concat = require("concat-stream");

request(url)
  .pipe(zlib.createGunzip())
  .pipe(
    concat((stringBuffer) => {
      console.log(stringBuffer.toString());
    })
  );

to call request to make a get request to the file url.

Then we call pipe with the stream returned by zlib.createGunzip() to unzip the file.

And we call pipe again with the stream returned by the concat function to get the unzipped data as a buffer from stringBuffer.

Categories
JavaScript Answers

How to use docker run command to pass arguments to CMD in Dockerfile?

To use docker run command to pass arguments to CMD in Dockerfile, we run CMD with an array.

For instance, we write

CMD ["sh", "-c", "node server.js ${cluster} ${environment}"]

in our Dockerfile to run the sh command with the arguments after it.

cluster and environment are the environment variables we defined in the docket-compose.yml file like

node:
  environment:
    - environment=dev
    - cluster=0
Categories
JavaScript Answers

How to install and run TypeScript locally in npm?

To install and run TypeScript locally in npm, we add a script to run the tsc command.

For instance, in package.json, we write

{
  "name": "foo",
  "scripts": {
    "tsc": "tsc"
  },
  "dependencies": {
    "typescript": "^1.8.10"
  }
}

to add the tsc script which runs the tsc command.

Then we run

npm run tsc 

to install the tsc command.

Categories
JavaScript Answers

How to add logging for an Express.js Node app?

To add logging for an Express.js Node app, we use the morgan package.

For instance, we write

const addRequestId = require("express-request-id")({ setHeader: false });
app.use(addRequestId);

const morgan = require("morgan");
morgan.token("id", (req) => req.id.split("-")[0]);

app.use(
  morgan("[:date[iso] #:id] Started :method :url for :remote-addr", {
    immediate: true,
  })
);

app.use(
  morgan(
    "[:date[iso] #:id] Completed :status :res[content-length] in :response-time ms"
  )
);

app.use("/api", router);

to use the express-request-id to add an ID to each request and make it available with the req.id property.

And thenm we call app.use with the middleware returned by morgan.

We call morgan with the format string for each log entry to add logging.

Categories
JavaScript Answers

How to fix the UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block error with Node?

To fix the UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block error with Node, we add a catch block.

For instance, we write

router.get("/emailfetch", authCheck, async (req, res, next) => {
  try {
    const emailFetch = await gmaiLHelper.getEmails(
      req.user._doc.profile_id,
      "/messages",
      req.user.accessToken
    );
    res.send(emailFetch.data);
  } catch (err) {
    next(err);
  }
});

to wrap our code that might thrown an error with a try block.

Then when an error is thrown, the code in the catch block will run.