Categories
JavaScript Answers

How to add gzip compression with Node.js?

Sometimes, we want to add gzip compression with Node.js.

In this article, we’ll look at how to add gzip compression with Node.js.

How to add gzip compression with Node.js?

To add gzip compression with Node.js, we can use the built in zlib module.

For instance, we write

const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
  const raw = fs.createReadStream('index.html');
  let acceptEncoding = request.headers['accept-encoding'];
  if (!acceptEncoding) {
    acceptEncoding = '';
  }

  if (acceptEncoding.match(/\bdeflate\b/)) {
    response.writeHead(200, {
      'content-encoding': 'deflate'
    });
    raw.pipe(zlib.createDeflate()).pipe(response);
  } else if (acceptEncoding.match(/\bgzip\b/)) {
    response.writeHead(200, {
      'content-encoding': 'gzip'
    });
    raw.pipe(zlib.createGzip()).pipe(response);
  } else {
    response.writeHead(200, {});
    raw.pipe(response);
  }
}).listen(3000);

to call createServer with a callback to create a web server.

In the callback, we call zlib.createDeflate and pipe with response to pipe the response for compress .

And we call response.writeHead with an object to set the content-encoding response header to 'gzip‘.

Then we use raw.pipe(zlib.createGzip()).pipe(response) to compress the response with gzip compression.

Conclusion

To add gzip compression with Node.js, we can use the built in zlib module.

Categories
JavaScript Answers

How to add dates to pm2 error logs with Node.js?

Sometimes, we want to add dates to pm2 error logs with Node.js.

In this article, we’ll look at how to add dates to pm2 error logs with Node.js.

How to add dates to pm2 error logs with Node.js?

To add dates to pm2 error logs with Node.js, we can run pm2 with the `–log-date-format option.

For instance, we run

pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS'

to run pm2 to start app.js and add dates to the log by specifying the --log-date-format option to add a date to each log entry in format DD-MM HH:mm:ss.SSS.

DD-MM HH:mm:ss.SSS is a moment format date string with the date, month, houts, minutes, seconds and milliseconds in this order.

Conclusion

To add dates to pm2 error logs with Node.js, we can run pm2 with the `–log-date-format option.

Categories
JavaScript Answers

How to get the parent directory name in Node.js?

Sometimes, we want to get the parent directory name in Node.js.

In this article, we’ll look at how to get the parent directory name in Node.js.

How to get the parent directory name in Node.js?

To get the parent directory name in Node.js, we can use the path.basename and path.dirname methods.

For instance, we write

path.basename(path.dirname(filename))

to get the folder name of the path of filename with path.dirname.

Then we call path.basename to get the parent folder path from the path string returned by path.dirname.

Conclusion

To get the parent directory name in Node.js, we can use the path.basename and path.dirname methods.

Categories
JavaScript Answers

How to protect the password field in MongoDB Mongoose so it won’t return in a query?

Sometimes, we want to protect the password field in MongoDB Mongoose so it won’t return in a query.

In this article, we’ll look at how to protect the password field in MongoDB Mongoose so it won’t return in a query.

How to protect the password field in MongoDB Mongoose so it won’t return in a query?

To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.

For instance, we write

const userSchema = new Schema({
  name: {
    type: String,
    required: false,
    minlength: 5
  },
  email: {
    type: String,
    required: true,
    minlength: 5
  },
  phone: String,
  password: {
    type: String,
    select: false
  },
})

to create the userSchema that has the password field set to type String.

And we add select and set it to false to prevent it from being returned in queries.

Conclusion

To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.

Categories
JavaScript Answers

How to modify the data returned by a Mongoose query in Node.js?

Sometimes, we want to modify the data returned by a Mongoose query in Node.js.

In this article, we’ll look at how to modify the data returned by a Mongoose query in Node.js.

How to modify the data returned by a Mongoose query in Node.js?

To modify the data returned by a Mongoose query in Node.js, we can call lean to make Mongoose return a plain object version of the query result that we can modify.

For instance, we write

Survey.findById(req.params.id).lean().exec((err, data) => {
  data.surveyQuestions.forEach((sq) => {
    Question.findById(sq.question, (err, q) => {
      sq.question = q;
    });
  });
});

to call lean after call findById to make the data in the exec callback a plain JavaScript object version of the query result.

We then loop through each item in data.surveyQuestions and set sq.question to q on each entry in the Question.findById callback.

Conclusion

To modify the data returned by a Mongoose query in Node.js, we can call lean to make Mongoose return a plain object version of the query result that we can modify.