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.

Categories
JavaScript Answers

How to access Express.js local variables in client side JavaScript?

Sometimes, we want to access Express.js local variables in client side JavaScript.

In this article, we’ll look at how to access Express.js local variables in client side JavaScript.

How to access Express.js local variables in client side JavaScript?

To access Express.js local variables in client side JavaScript, we can call res.render with an object with the variables.

Then we can reference them in the template.

For instance, we write

res.render('search-directory', {
  title: 'My Title',
  placeUrls: JSON.stringify(placeUrls),
});

to call res.render with an object with the local variables in the 2nd argument.

Then we write

search-directory.jade

script(type='text/javascript').
    const urls =!{placeUrls}

to add a script tag which assigns urls to the placeUrls JSON string.

The string will be interpolate as a JavaScript object.

Conclusion

To access Express.js local variables in client side JavaScript, we can call res.render with an object with the variables.