Categories
JavaScript Answers

How to create a proxy with Express.js?

Sometimes, we want to create a proxy with Express.js.

In this article, we’ll look at how to create a proxy with Express.js.

How to create a proxy with Express.js?

To create a proxy with Express.js, we can use the express-http-proxy package.

To install it, we run

npm i express-http-proxy

Then we write

const url = require('url');
const proxy = require('express-http-proxy');

const apiProxy = proxy('example.com:3000/test', {
  proxyReqPathResolver: req => url.parse(req.baseUrl).path
});

to call proxy with the URL we want to proxy and an object that has the proxyReqPathResolver property set to a function to map the URL we’re proxy to a URL in our app.

url.parse parses the URL and we get the URL path with path.

Conclusion

To create a proxy with Express.js, we can use the express-http-proxy package.

Categories
JavaScript Answers

How to return the current timestamp with Moment.js?

Sometimes, we want to return the current timestamp with Moment.js.

In this article, we’ll look at how to return the current timestamp with Moment.js.

How to return the current timestamp with Moment.js?

To return the current timestamp with Moment.js, we can use the unix method.

For instance, we write

const currentDate = moment().unix();

to return the current UNIX timestamp.

Conclusion

To return the current timestamp with Moment.js, we can use the unix method.

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.