Categories
JavaScript Answers

How to send response to all clients except sender with Node.js socket.io?

Sometimes, we want to send response to all clients except sender with Node.js socket.io.

In this article, we’ll look at how to send response to all clients except sender with Node.js socket.io.

For instance, we write

socket.broadcast.emit('message', "this is a test");

to send to all clients the 'this is a test' message.

And we write

socket.broadcast.to('game').emit('message', 'nice game');

to send to all clients in the game room the message 'nice game' except to the sender.

Conclusion

To send response to all clients except sender with Node.js socket.io, we call socket.broadcast.emit to send to all clients except the sender.

And we use socket.broadcast.to to send to all clients in a room except the sender.

Categories
JavaScript Answers

How to consume the JSON POST data in an Express application?

Sometimes, we want to consume the JSON POST data in an Express application.

In this article, we’ll look at how to consume the JSON POST data in an Express application.

How to consume the JSON POST data in an Express application?

To consume the JSON POST data in an Express application, we call app.post.

For instance, we write

const express = require('express');

const app = express();

app.use(express.json());

app.post('/', (request, response) => {
  console.log(request.body);
  response.send(request.body);
});

app.listen(3000);

to call app.post with the route URL and the route handler callback.

In the callback, we get the request body from request.body.

request.body is available since we use the middleware returned by express.json with

app.use(express.json());

Conclusion

To consume the JSON POST data in an Express application, we call app.post.

Categories
JavaScript Answers

How to get the hostname of current request in Node.js Express?

Sometimes, we want to get the hostname of current request in Node.js Express.

In this article, we’ll look at how to get the hostname of current request in Node.js Express.

How to get the hostname of current request in Node.js Express?

To get the hostname of current request in Node.js Express, we can use the os.hostname method.

For instance, we write

const os = require("os");
os.hostname();

to call os.hostname to return the hostname of the current request.

Conclusion

To get the hostname of current request in Node.js Express, we can use the os.hostname method.

Categories
JavaScript Answers

How to make Axios send cookies in its requests automatically?

Sometimes, we want to make Axios send cookies in its requests automatically.

In this article, we’ll look at how to make Axios send cookies in its requests automatically.

How to make Axios send cookies in its requests automatically?

To make Axios send cookies in its requests automatically, we can set the withCredentials option to true.

For instance, we write

axios.get(BASE_URL + '/todos', {
  withCredentials: true
});

to call axios.get with an object that has withCredentials set to true to send the cookie with the request to BASE_URL + '/todos'.

Conclusion

To make Axios send cookies in its requests automatically, we can set the withCredentials option to true.

Categories
JavaScript Answers

How to sort in Node.js Mongoose?

Sometimes, we want to sort in Node.js Mongoose.

In this article, we’ll look at how to sort in Node.js Mongoose.

How to sort in Node.js Mongoose?

To sort in Node.js Mongoose, we can use the sort method.

For instance, write:

Post.find({}).sort('test').exec((err, docs) => {
  //...
});

to call sort with the property to sort by.

We can sort descending with

Post.find({}).sort([
  ['date', -1]
]).exec((err, docs) => {
  //...
});

We call sort with a nested array with the property name and -1.

The exec callback is run when the sorting is done.

docs has the sorted data.

Conclusion

To sort in Node.js Mongoose, we can use the sort method.