Categories
JavaScript Answers

How to add timestamps to all console messages with Express and Node.js?

Sometimes, we want to add timestamps to all console messages with Express and Node.js.

In this article, we’ll look at how to add timestamps to all console messages with Express and Node.js.

How to add timestamps to all console messages with Express and Node.js?

To add timestamps to all console messages with Express and Node.js, we can customize the Express logger with express.logger.format.

For instance, we write

const df = require('console-stamp/node_modules/dateformat');

express.logger.format('mydate', () => {
  return df(new Date(), 'HH:MM:ss.l');
});

app.use(express.logger('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));

to require the console-stamp module.

We install it by running

npm i console-stamp

Then we call express.logger.format with the name of the logger and a function to returns the logger formatter object by calling df to include the date in the log entries.

Then we call express.logger to return the logging middleware with the custom format.

And we call app.use to use the logger middleware.

Conclusion

To add timestamps to all console messages with Express and Node.js, we can customize the Express logger with express.logger.format.

Categories
JavaScript Answers

How to pass an app instance to routes from a different file with Express?

Sometimes, we want to pass an app instance to routes from a different file with Express.

In this article, we’ll look at how to pass an app instance to routes from a different file with Express.

How to pass an app instance to routes from a different file with Express?

To pass an app instance to routes from a different file with Express, we can call require on the module with the routes in the module that we defined the app instance in.

For instance, we write

app.js

const app = module.exports = express();
app.use(app.router);

require('./routes');

to export the app instance created by express.

Then in routes/index.js, we write

const app = require('../app');

app.get('/', (req, res, next) => {
  res.render('index');
});

require('./user');
require('./blog');

to require app.js and then call app.get to add a GET route.

We also call require with other route modules which require app individually themselves to add the routes from those files.

Conclusion

To pass an app instance to routes from a different file with Express, we can call require on the module with the routes in the module that we defined the app instance in.

Categories
JavaScript Answers

How to get response from S3 getObject in Node.js?

Sometimes, we want to get response from S3 getObject in Node.js.

In this article, we’ll look at how to get response from S3 getObject in Node.js.

How to get response from S3 getObject in Node.js?

To get response from S3 getObject in Node.js, we can get the response data from the callback.

For instance, we write

const aws = require('aws-sdk');
const s3 = new aws.S3();

const getParams = {
  Bucket: 'abc',
  Key: 'abc.txt'
}

s3.getObject(getParams, (err, data) => {
  if (err) {
    return;
  }
  const objectData = data.Body.toString('utf-8');
  //...
});

to call s3.getObject with the parameters for the file we want to get.

Then we pass in a callback that has the retrieved file.

The file data is in the data parameter.

And we can read it as a string with data.Body.toString with the encoding of the file.

Conclusion

To get response from S3 getObject in Node.js, we can get the response data from the callback.

Categories
JavaScript Answers

How to add wildcard routing to cover everything under and including a path with Express.js?

Sometimes, we want to add wildcard routing to cover everything under and including a path with Express.js.

In this article, we’ll look at how to add wildcard routing to cover everything under and including a path with Express.js.

How to add wildcard routing to cover everything under and including a path with Express.js?

To add wildcard routing to cover everything under and including a path with Express.js, we can use the * wildcard character in our route paths.

For instance, we write

const express = require("express")
const app = express.createServer();

const fooRoute = (req, res, next) => {
  res.end("foo route");
}

app.get("/foo*", fooRoute);
app.get("/foo", fooRoute);

app.listen(3000);

to call app.get with "/foo*" to add a route that matches any path that starts with /foo.

The 2nd route we add with app.get matches only the path /foo.

Conclusion

To add wildcard routing to cover everything under and including a path with Express.js, we can use the * wildcard character in our route paths.

Categories
JavaScript Answers

How to send a message to a particular client with Node.js socket.io?

Sometimes,. we want to send a message to a particular client with Node.js socket.io.

In this article, we’ll look at how to send a message to a particular client with Node.js socket.io.

How to send a message to a particular client with Node.js socket.io?

To send a message to a particular client with Node.js socket.io, we can call sockets.in before calling emit.

For instance, we write

const socket = io.connect('http://localhost');
socket.emit('join', {
  email: 'user@example.com'
});

on client side to connect to the socket.io server.

Then on server side, we write

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

io.sockets.on('connection', (socket) => {
  socket.on('join', (data) => {
    socket.join(data.email);
    io.sockets.in('user@example.com').emit('msg', {
      msg: 'hello'
    })
  });
});

to create a socket.io server.

And we listen for 'join' events that are emitted by clients.

We get the data from the object in the 2nd argument of emit from data.

Then we call io.sockets.in with 'user@example.com' and then call emit to send data to the client with email 'user@example.com'

Conclusion

To send a message to a particular client with Node.js socket.io, we can call sockets.in before calling emit.