Categories
JavaScript Answers

How to fix the “Error: Cannot find module ‘ejs'” error with Node.js?

Sometimes, we want to fix the "Error: Cannot find module ‘ejs’" error with Node.js.

In this article, we’ll look at how to fix the "Error: Cannot find module ‘ejs’" error with Node.js.

How to fix the "Error: Cannot find module ‘ejs’" error with Node.js?

To fix the "Error: Cannot find module ‘ejs’" error with Node.js, we can install the ejs package.

To install it, we run

npm i ejs

Then we use it with

const ejs = require('ejs');

//...

app.set('view engine','ejs'); 
app.engine('ejs', require('ejs').__express);

to require the ejs module and call app.set to set the view engine to ejs. And we call app.engine with define the ejs view engine by require the module and returning the __express property.

Conclusion

To fix the "Error: Cannot find module ‘ejs’" error with Node.js, we can install the ejs package.

Categories
JavaScript Answers

How to output ‘id’ instead of ‘_id’ with MongoDB?

Sometimes, we want to output ‘id’ instead of ‘_id’ with MongoDB.

In this article, we’ll look at how to output ‘id’ instead of ‘_id’ with MongoDB.

How to output ‘id’ instead of ‘_id’ with MongoDB?

To output ‘id’ instead of ‘_id’ with MongoDB, we call schema’s virtual method to add a new computed field and return the _id value with the field.

And then we call set to set virtuals to true to return the field.

For instance, we write

Schema.virtual('id').get(function() {
  return this._id.toHexString();
});

Schema.set('toJSON', {
  virtuals: true
});

to call virtual with 'id' to create the id field.

And we call get with a function to return the hex string version of the _id with toHexString.

Finally, we call set with 'toJSON' and an object with virtuals set to true to return the id property with the result.

Conclusion

To output ‘id’ instead of ‘_id’ with MongoDB, we call schema’s virtual method to add a new computed field and return the _id value with the field.

And then we call set to set virtuals to true to return the field.

Categories
JavaScript Answers

How to add custom certificate authority (CA) to Node.js?

Sometimes, we want to add custom certificate authority (CA) to Node.js.

In this article, we’ll look at how to add custom certificate authority (CA) to Node.js.

How to add custom certificate authority (CA) to Node.js?

To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push with the certificate file.

For instance, we write

const trustedCa = [
  '/etc/pki/tls/certs/ca-bundle.crt',
  '/path/to/custom/cert.crt'
];

https.globalAgent.options.ca = [];
for (const ca of trustedCa) {
  https.globalAgent.options.ca.push(fs.readFileSync(ca));
}

We loop through the path strings in trustedCa with a for-of loop.

Then we call https.globalAgent.options.ca.push to push the certificate file that we read into memory with fs.readFileSync.

We set https.globalAgent.options.ca to an array before calling push so its value is always an array.

Conclusion

To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push with the certificate file.

Categories
JavaScript Answers

How to get the object ID after an object is saved in Mongoose?

Sometimes, we want to get the object ID after an object is saved in Mongoose.

In this article, we’ll look at how to get the object ID after an object is saved in Mongoose.

How to get the object ID after an object is saved in Mongoose?

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that’s run after we call save.

For instance, we write

const {
  Schema
} = require('mongoose')

mongoose.connect('mongodb://localhost/lol', (err) => {
  if (err) {
    console.log(err)
  }
});

const ChatSchema = new Schema({
  name: String
});

mongoose.model('Chat', ChatSchema);

const Chat = mongoose.model('Chat');

const n = new Chat();
n.name = "chat room";
n.save((err, room) => {
  console.log(room._id);
});

to create a schema with the Schema constructor.

Next, we register that Chat model with mongoose.model.

Then create a new document with the Chat model.

Finally, we call save with the saved document data set as the value of room.

And we get the object ID of the saved document from room._id.

Conclusion

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that’s run after we call save.

Categories
JavaScript Answers

How to parse query string in Node.js?

Sometimes, we want to parse query string in Node.js.

In this article, we’ll look at how to parse query string in Node.js.

How to parse query string in Node.js?

To parse query string in Node.js, we can use the url module.

For instance, we write

const http = require('http');
const url = require('url');

const server = http.createServer((request, response) => {
  const {
    query: queryData
  } = url.parse(request.url, true);
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });

  if (queryData.name) {
    response.end(queryData.name);
  } else {
    response.end("Hello World");
  }
});

server.listen(8000);

to call url.parse to parse the request.url value in the browser.

And we get the parsed query string values from the query property from the returned object.

Then we get the name query parameter value from queryData.name.

Conclusion

To parse query string in Node.js, we can use the url module.