Categories
JavaScript Answers

How to use the Node.js Morgan logger?

Sometimes, we want to use the Node.js Morgan logger.

In this article, we’ll look at how to use the Node.js Morgan logger.

How to use the Node.js Morgan logger?

To use the Node.js Morgan logger, we can use it with log4js.

For instance, we write

const express = require("express");
const log4js = require("log4js");
const morgan = require("morgan");

//...

const theAppLog = log4js.getLogger();
const theHTTPLog = morgan({
  "format": "default",
  "stream": {
    write: (str) => {
      theAppLog.debug(str);
    }
  }
});

//...

const theServer = express();
theServer.use(theHTTPLog);

to create a logger instance with log4js.getLogger.

Then we call morgan with an object sets some options.

We set stream.write to a function that calls theAppLog.debug to use log4js with morgan to do the logging.

And then we call use with theHTTPLog to use the logger created by morgan to do the logging.

Conclusion

To use the Node.js Morgan logger, we can use it with log4js.

Categories
JavaScript Answers

How to remove debugging from socket.io?

Sometimes, we want to remove debugging from socket.io.

In this article, we’ll look at how to remove debugging from socket.io.

How to remove debugging from socket.io?

To remove debugging from socket.io, we can set the log option to false when we call listen.

For instance, we write

const io = require('socket.io').listen(app, {
  log: false
});

to call listen with log set to false to disable logging with socket.io.

We can also set the log level with

const io = require('socket.io').listen(app);

io.set('log level', 1);

to set log level to 1 to reduce the level of logging.

Conclusion

To remove debugging from socket.io, we can set the log option to false when we call listen.

Categories
JavaScript Answers

How to set up two different static directories with Node.js Express framework?

Sometimes, we want to set up two different static directories with Node.js Express framework.

In this article, we’ll look at how to set up two different static directories with Node.js Express framework.

How to set up two different static directories with Node.js Express framework?

To set up two different static directories with Node.js Express framework, we can call express.static and app.use with different static file paths.

For instance, we write

app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));

to expose the /public URL path to __dirname + "/public" with express.static.

Likewise, we expose the /public2 URL path to __dirname + "/public2" with express.static.

Conclusion

To set up two different static directories with Node.js Express framework, we can call express.static and app.use with different static file paths.

Categories
JavaScript Answers

How to determine if a string is a MongoDB ObjectID?

Sometimes, we want to determine if a string is a MongoDB ObjectID.

In this article, we’ll look at how to determine if a string is a MongoDB ObjectID.

How to determine if a string is a MongoDB ObjectID?

To determine if a string is a MongoDB ObjectID, we can use the mongoose.isValidObjectId method.

For instance, we write

mongoose.isValidObjectId(string);

to call mongoose.isValidObject with the string that we want to check if it’s an ObjectID.

It returns true if string is a valid ObjectID and false otherwise.

Conclusion

To determine if a string is a MongoDB ObjectID, we can use the mongoose.isValidObjectId method.

Categories
JavaScript Answers

How to combine two OR-queries with AND in Mongoose?

Sometimes, we want to combine two OR-queries with AND in Mongoose.

In this article, we’ll look at how to combine two OR-queries with AND in Mongoose.

How to combine two OR-queries with AND in Mongoose?

To combine two OR-queries with AND in Mongoose, we can use the find method.

For instance we write

Test.find({
  $and: [{
      $or: [{
        a: 1
      }, {
        b: 1
      }]
    },
    {
      $or: [{
        c: 1
      }, {
        d: 1
      }]
    }
  ]
}, (err, results) => {
  //...
})

to call Test.find with the $and property set to an array with 2 objects with $or properties.

This combines the 2 OR clauses with AND.

We get the query results from results.

Conclusion

To combine two OR-queries with AND in Mongoose, we can use the find method.