Categories
JavaScript Answers

How to add logging with Node.js?

Sometimes, we want to add logging with Node.js.

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

How to add logging with Node.js?

To add logging with Node.js, we can use the winston package.

We install it by running

npm i winston

Then we use it by writing

const winston = require('winston');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: {
    service: 'user-service'
  },
  transports: [
    new winston.transports.File({
      filename: 'error.log',
      level: 'error'
    }),
    new winston.transports.File({
      filename: 'combined.log'
    }),
  ],
});

We create a new winston logger using the winston.Logger constructor.

We call the constructor with an object that lets us set the transports option to specify where to write the log output.

The transports option is set to an array that specifies we write log items with level error to error.log.

And we write everything else to combined.log.

The format is set to winston.format.json so the log entries are in JSON format.

And the default log item level is 'info'.

We then log entries with

logger.info('log to file');

to log something with level info.

Conclusion

To add logging with Node.js, we can use the winston package.

Categories
JavaScript Answers

How to use Node.js and socket.io with SSL?

Sometimes, we want to use Node.js and socket.io with SSL.

In this article, we’ll look at how to use Node.js and socket.io with SSL.

How to use Node.js and socket.io with SSL?

To use Node.js and socket.io with SSL, we can set the secure option to true when we call io.connect.

For instance, we write

const socket = io.connect('https://localhost', {
  secure: true
});

to call io.connect with the socket.io server URL and the secure option set to true to connect via SSL.

Conclusion

To use Node.js and socket.io with SSL, we can set the secure option to true when we call io.connect.

Categories
JavaScript Answers

How to use Node.js filesystem with async / await?

Sometimes, we want to use Node.js filesystem with async / await.

In this article, we’ll look at how to use Node.js filesystem with async / await.

How to use Node.js filesystem with async / await?

To use Node.js filesystem with async / await, we can use the fs.promises module.

For instance, we write

import fs from 'fs';
const fsPromises = fs.promises;

const listDir = async () => {
  try {
    return fsPromises.readdir('path/to/dir');
  } catch (err) {
    console.error('Error occured while reading directory!', err);
  }
}

listDir();

to define the listDir function that calls fsPromises.readdir with the path we want to list.

We use await to get the result resolved by the promise returned.

And we use try and catch to catch any errors if the promise is rejected.

Conclusion

To use Node.js filesystem with async / await, we can use the fs.promises module.

Categories
JavaScript Answers

How to add error handling to Node.js streams?

Sometimes, we want to add error handling to Node.js streams.

In this article, we’ll look at how to add error handling to Node.js streams.

How to add error handling to Node.js streams?

To add error handling to Node.js streams, we call pipeline with all the operators before the error handler.

For instance, we write

const {
  pipeline,
  finished
} = require('stream');

pipeline(
  input,
  transformA,
  transformB,
  transformC,
  (err) => {
    if (err) {
      console.error('Pipeline failed', err);
    } else {
      console.log('Pipeline succeeded');
    }
  });


finished(input, (err) => {
  if (err) {
    console.error('Stream failed', err);
  } else {
    console.log('Stream is done reading');
  }
});

to call pipeline with the operators input, transformA, and transformB, and transformC.

Then we pass in the error handler callback so that errors with any of the operators will be picked up by it.

Then we call finished with input to handle any errors with input.

Conclusion

To add error handling to Node.js streams, we call pipeline with all the operators before the error handler.

Categories
JavaScript Answers

How to remove menu bar from Electron app?

Sometimes, we want to remove menu bar from Electron app.

In this article, we’ll look at how to remove menu bar from Electron app.

How to remove menu bar from Electron app?

To remove menu bar from Electron app, we can set the autoHideMenuBar to true.

For instance, we write

const mainWindow = new BrowserWindow({
  autoHideMenuBar: true,
})

to create a BrowserWindow instance by passing in an object with autoHideMenuBar set to true.

As a result, the menu bar would be hidden in the app.

Conclusion

To remove menu bar from Electron app, we can set the autoHideMenuBar to true.