Categories
JavaScript Answers

How to change default layout in Express using Handlebars?

Sometimes, we want to change default layout in Express using Handlebars.

In this article, we’ll look at how to change default layout in Express using Handlebars.

How to change default layout in Express using Handlebars?

To change default layout in Express using Handlebars, we can res.render with the layout option.

For instance, we write

res.render('view', {
  title: 'my other page',
  layout: 'other'
});

to call res.render with the layout set to other to render other.hbs as the layout file.

We can override the layout for the whole app with app.set:

app.set('view options', { layout: 'other' });

Conclusion

To change default layout in Express using Handlebars, we can res.render with the layout option.

Categories
JavaScript Answers

How to create a pair of private and public keys using Node.js crypto?

Sometimes, we want to create a pair of private and public keys using Node.js crypto.

In this article, we’ll look at how to create a pair of private and public keys using Node.js crypto.

How to create a pair of private and public keys using Node.js crypto?

To create a pair of private and public keys using Node.js crypto, we can use the generateKeyPair function.

For instance, we write

const {
  generateKeyPair
} = require('crypto');

generateKeyPair('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret'
  }
}, (err, publicKey, privateKey) => {
  //...
});

to call generateKeyPair with 'rsa' to generate a pair of RSA keys.

We set the settings for the public key in publicKeyEncoding and set the settings for the private key in privateKeyEncoding.

Then we get the public and private from the publicKey and privateKey parameter in the callback

Conclusion

To create a pair of private and public keys using Node.js crypto, we can use the generateKeyPair function.

Categories
JavaScript Answers

How to get only the dataValues from Sequelize ORM?

Sometimes, we want to get only the dataValues from Sequelize ORM.

In this article, we’ll look at how to get only the dataValues from Sequelize ORM.

How to get only the dataValues from Sequelize ORM?

To get only the dataValues from Sequelize ORM, we can set the raw option to true.

For instance, we write

Model.findAll({
  raw: true,
  //...
});

to call findAll with an object with the raw property set to true to only return the dataValues in the query results.

Conclusion

To get only the dataValues from Sequelize ORM, we can set the raw option to true.

Categories
JavaScript Answers

How to update and return document in MongoDB?

Sometimes, we want to update and return document in MongoDB.

In this article, we’ll look at how to update and return document in MongoDB.

How to update and return document in MongoDB?

To update and return document in MongoDB, we can call findAndUpdare with the returnOriginal option set to false.

For instance, we write

collection.findOneAndUpdate({
    code
  }, {
    $set: updatedFields
  }, {
    returnOriginal: false
  },
  (err, documents) => {
    //...
    db.close();
  }
);

to call findOneAndUpdate to update the entries with code set to code with the updatedFields object values.

We set returnOriginal to false so that documents in the callback would return the updated versions of the updated documents.

Finally, we call db.close to close the database connection.

Conclusion

To update and return document in MongoDB, we can call findAndUpdare with the returnOriginal option set to false.

Categories
JavaScript Answers

How to create rooms in socket.io?

Sometimes, we want to create rooms in socket.io.

In this article, we’ll look at how to create rooms in socket.io.

How to create rooms in socket.io?

To create rooms in socket.io, we can just listen for events and call socket.join to join the room.

For instance, we write

const socket = io.connect();
socket.emit('create', 'room1');

to call socket.emit with 'create' and 'room1' to emit the create event and room room1 from client side.

Then on server side, we write

io.sockets.on('connection', (socket) => {
  socket.on('create', (room) => {
    socket.join(room);
  });
});

to call sockets.on to listen to the connection event.

Then in the connection event handler, we call socket.on again to listen to the 'create' event that we get from client side.

In it, we call socket.join with the room, which should be set to 'room1' with socket.emit to join the 'room1'.

Conclusion

To create rooms in socket.io, we can just listen for events and call socket.join to join the room.