Categories
JavaScript Answers

How to wrap a buffer as a stream2 Readable stream with Node.js?

Sometimes, we want to wrap a buffer as a stream2 Readable stream with Node.js.

In this article, we’ll look at how to wrap a buffer as a stream2 Readable stream with Node.js.

How to wrap a buffer as a stream2 Readable stream with Node.js?

To wrap a buffer as a stream2 Readable stream with Node.js, we can use the stream.PassThrough method.

For instance, we write

const stream = require('stream');

const bufferStream = new stream.PassThrough();
bufferStream.end(Buffer.from('Test data.'));
bufferStream.pipe(process.stdout)

to call stream.PassThrough to create a new pass through stream instance.

Then we call end with a buffer to write the buffer into the stream.

And then we call pipe to pipe the stream into stdout.

Conclusion

To wrap a buffer as a stream2 Readable stream with Node.js, we can use the stream.PassThrough method.

Categories
JavaScript Answers

How to use Node.js Winston in several modules?

Sometimes, we want to use Node.js Winston in several modules.

In this article, we’ll look at how to use Node.js Winston in several modules.

How to use Node.js Winston in several modules?

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.

For instance, we create the log.js module by writing

const winston = require('winston');

winston.configure({
  level: "debug",
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.simple()
  ),
  transports: [
    new winston.transports.Console()
  ]
});

Then we in the app.js entry point file, we add

require('log.js')

And in other modules, we write

require ('winston')

so we can use the logger in any module.

Conclusion

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.

Categories
JavaScript Answers

How to get all the values that contains part of a string using Mongoose find?

Sometimes, we want to get all the values that contains part of a string using Mongoose find.

In this article, we’ll look at how to get all the values that contains part of a string using Mongoose find.

How to get all the values that contains part of a string using Mongoose find?

To get all the values that contains part of a string using Mongoose find, we can use the $regex operator.

For instance, we write

Books.find({
    "authors": {
      "$regex": "Alex",
      "$options": "i"
    }
  },
  (err, docs) => {}
);

to call Books.find to query for authors set to a string that has 'Alex' in it anywhere in the string.

We set $options to 'i' to compare authors in a case-insensitive manner.

Conclusion

To get all the values that contains part of a string using Mongoose find, we can use the $regex operator.

Categories
JavaScript Answers

How to read an Excel file using Node.js?

Sometimes, we want to read an Excel file using Node.js.

In this article, we’ll look at how to read an Excel file using Node.js.

How to read an Excel file using Node.js?

To read an Excel file using Node.js, we can use the node-xlsx module.

To install it, we run

npm i node-xlsx

Then we can use it by writing

const xlsx = require('node-xlsx');

const obj1 = xlsx.parse(__dirname + '/myFile.xlsx');
const obj2 = xlsx.parse(fs.readFileSync(__dirname + '/myFile.xlsx'));

to call xlsx.parse with the path of the Excel file and assign the returned object to obj1.

Then we call xlsx.parse with a buffer returned from reading the Excel file with readFileSync and assign that to obj2.

Conclusion

To read an Excel file using Node.js, we can use the node-xlsx module.

Categories
JavaScript Answers

How to set a base URL for Node.js Express app?

Sometimes, we want to set a base URL for Node.js Express app.

In this article, we’ll look at how to set a base URL for Node.js Express app.

How to set a base URL for Node.js Express app?

To set a base URL for Node.js app, we can call app.use with the base URL` and a route object.

For instance, we write

const express = require('express');
const app = express();
const router = express.Router();

router.use((req, res, next) => {
  console.log('%s %s %s', req.method, req.url, req.path);
  next();
});

router.use('/bar', (req, res, next) => {
  next();
});

router.use((req, res, next) => {
  res.send('Hello World');
});

app.use('/foo', router);

app.listen(3000);

to create a new router with express.Router.

Then we add some middlewares and routes with router.use.

And then we call app.use with '/foo' and router to nest router with /foo as the base URL for the routes.

Conclusion

To set a base URL for Node.js app, we can call app.use with the base URL` and a route object.