Categories
JavaScript Answers

How to make join queries using Sequelize on Node.js?

Sometimes, we want to make join queries using Sequelize on Node.js.

In this article, we’ll look at how to make join queries using Sequelize on Node.js.

How to make join queries using Sequelize on Node.js?

To make join queries using Sequelize on Node.js, we can use the include option with findAll.

For instance, we write

const posts = await Posts.findAll({
  include: [{
    model: User,
    required: true
  }]
})

//...

to call Posts.findAll with an object that has the include property set to an array with the model set to User to join the User entity when we make a query for Posts.

required is set to true so we do an inner join.

To do a left outer join, we set required to false.

Conclusion

To make join queries using Sequelize on Node.js, we can use the include option with findAll.

Categories
JavaScript Answers

How to get the client’s IP address in socket.io?

Sometimes, we want to get the client’s IP address in socket.io.

In this article, we’ll look at how to get the client’s IP address in socket.io.

How to get the client’s IP address in socket.io?

To get the client’s IP address in socket.io, we can use the socket.handshake.address property.

For instance, we write

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

io.sockets.on('connection', (socket) => {
  const {
    address
  } = socket.handshake;
  console.log(address.address, address.port);
});

to call io.sockets.on with 'connection' to listen for client connections.

Then callback runs when a client is connected.

In it, we use socket.handshake.address to get the client’s address.

And we use address.address to get the IP address and address.port to get the port used for the connection.

Conclusion

To get the client’s IP address in socket.io, we can use the socket.handshake.address property.

Categories
JavaScript Answers

How to spawn child process and get terminal output live with Node.js?

Sometimes, we want to spawn child process and get terminal output live with Node.js.

In this article, we’ll look at how to spawn child process and get terminal output live with Node.js.

How to spawn child process and get terminal output live with Node.js?

To spawn child process and get terminal output live with Node.js, we can use the child_process module’s spawn method.

For instance, we write

const spawn = require('child_process').spawn;
const child = spawn('node ./commands/server.js');
let scriptOutput = "";

child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
  console.log('stdout: ' + data);
  data = data.toString();
  scriptOutput += data;
});

child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
  console.log('stderr: ' + data);
  data = data.toString();
  scriptOutput += data;
});

child.on('close', (code) => {
  console.log('closing code: ' + code);
  console.log('Full output of script: ', scriptOutput);
});

to call spawn to run 'node ./commands/server.js'.

Then we call child.stdout.on with 'data' to listen for stdout outputs.

In the function we pass into child.stdout.on , we log the output data with console.log and append the data to scriptOutput after it’s converted to a string.

Likewise, we call child.stderr.on to listen to stderr output by calling child.stderr.on with 'data'.

The callback is the same.

Finally, we call child.on with 'close' to get the exit code when the child process finishes running.

Conclusion

To spawn child process and get terminal output live with Node.js, we can use the child_process module’s spawn method.

Categories
JavaScript Answers

How to execute an external program from within Node.js?

Sometimes, we want to execute an external program from within Node.js.

In this article, we’ll look at how to execute an external program from within Node.js.

How to execute an external program from within Node.js?

To execute an external program from within Node.js, we can use the child_process module’s exec method.

For instance, we write

const {
  exec
} = require('child_process');

exec(command, (error, stdout, stderr) => {
  console.log(error, stdout, stderr)
});

to call exec with the command string we want to run.

The 2nd argument is a callback that has the stdout and stderr output after running the command.

error has the error that’s thrown when the command is run.

Conclusion

To execute an external program from within Node.js, we can use the child_process module’s exec method.

Categories
JavaScript Answers

How to delete a key from a MongoDB document using Mongoose?

Sometimes, we want to delete a key from a MongoDB document using Mongoose.

In this article, we’ll look at how to delete a key from a MongoDB document using Mongoose.

How to delete a key from a MongoDB document using Mongoose?

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.

For instance, we write

User.update({
  _id: user._id
}, {
  $unset: {
    field: 1
  }
}, callback);

to call update on the entry with the given _id value.

Then we use the $unset operator to remove the field property from the User entry.

The callback is a function that runs when the update operation is done.

Conclusion

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.