Node.js Tips

Node.js Tips — Upsert Data, Ciphers, and Parsing Directories

Spread the love

Like any kind of apps, there are difficult issues to solve when we write Node apps.

In this article, we’ll look at some solutions to common problems when writing Node apps.

Difference Between response.send and response.write in Express

res.send is called only once and it sends data in the chunk of the buffer size to the client.

Then it comes back and sends more data to the client with the buffer size until the sending is done.

res.write is called multiple times followed by res.end . It creates a buffer with the size based on the whole piece of data.

Then it sends that over HTTP. Therefore, it’s faster in case of huge amount of data.

Create Document if not Exists and Update the Existing Document Otherwise with Mongoose

We can create document if it doesn’t exist and do update otherwise with the findOneAndUpdate method.

For instance, we can write:

const query = {};
const update = { expire: new Date() };
const options = {
  upsert: true

Model.findOneAndUpdate(query, update, options, (error, result) => {
  if (error) return;

We pass in an options object with the upsert property set to true .

update has the data that we want to add or update.

query has the query for getting the data.

The callback is called when the operation is finished.

result has the data for the written file.

error has the error object.

Convert a Directory Structure in the Filesystem to JSON with Node.js

We can use the directory-package to read the structure of the filesystem into JSON.

To install it, we run:

npm i directory-tree

Then we can use it by writing:

const dirTree = require("directory-tree");
const tree = dirTree("/foo/bar");

We use the directory-tree package’s dirTree function.

Then we pass in the path into the dirTree function.

We can restrict the file types that we want to enumerate by writing:

const dirTree = require("directory-tree");
const filteredTree = dirTree("/some/path", { extensions: /.txt/ });

We use the extension regex to do the filtering.

Listen on HTTP and HTTPS for a Single Express App

We can listen to HTTP and HTTPS at the same time by using the http and https packages and calling the createServer method on both.

For instance, we can write:

const express = require('express');
const https = require('https');
const http = require('http');
const fs = require('fs');
const app = express();

const options = {
  key: fs.readFileSync('/path/to/key.pem'),
  cert: fs.readFileSync('/path/to/cert.pem'),
  ca: fs.readFileSync('/path/to/ca.pem')

https.createServer(options, app).listen(443);

We read the certificate files and call createServer with them to listen to HTTPS requests.

And we also use http to listen to regular HTTP requests as usual.

Use Cases for process.nextTick in Node.js

We can use process.nextTick to put a callback into a queue.

It’s the same as using setTimeout to delay code execution.

For instance, we can write:

process.nextTick(() => {

Encrypting Data that Needs to be Decrypted

We can encrypt data that needs to be decrypted with the crypto module.

It has the createCipher method to lets us create a cipher that can be deciphered.

For instance, we can write:

const crypto = require('crypto');

const algorithm = 'aes256';
const key = 'sercetkey';
const text = 'hello world';

const cipher = crypto.createCipher(algorithm, key);
const encrypted = cipher.update(text, 'utf8', 'hex') +'hex');

const decipher = crypto.createDecipher(algorithm, key);
const decrypted = decipher.update(encrypted, 'hex', 'utf8') +'utf8');

We called createCipher with the algorithm and key to create the cipher.

Then to encrypt the text , we call cipher.update and concatenate it with'hex') . can only be called once so that the text can only be encrypted once.

When we wan to decipher the text, we call the createDecipher method with the same algorithm and key .

Then we can use decipher.update to decipher the encrypted text and concatenate that with decipher.find('hex') to decrypt the text. can only be called once so that the encrypted text can only be decrypted once.


We can create ciphers and decipher text with the crypto module.

res.send and res.write are different in Express. One is good for small data and one is good for bigger ones.

We can upsert data with Mongoose.

Directory structures can be read into JSON.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *