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.

Categories
JavaScript Answers

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

Sometimes, we want to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

In this article, we’ll look at how to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

For instance, we write

const app = express();

app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({
  extended: true
}));
app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

to call passport.initialize and call app.use with the returned middleware before we call passport.session.

This way, Passport is initialized before we do anything else with Passport, and we avoid the error.

Conclusion

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

Categories
JavaScript Answers

How to use the Node.js Morgan logger?

Sometimes, we want to use the Node.js Morgan logger.

In this article, we’ll look at how to use the Node.js Morgan logger.

How to use the Node.js Morgan logger?

To use the Node.js Morgan logger, we can use it with log4js.

For instance, we write

const express = require("express");
const log4js = require("log4js");
const morgan = require("morgan");

//...

const theAppLog = log4js.getLogger();
const theHTTPLog = morgan({
  "format": "default",
  "stream": {
    write: (str) => {
      theAppLog.debug(str);
    }
  }
});

//...

const theServer = express();
theServer.use(theHTTPLog);

to create a logger instance with log4js.getLogger.

Then we call morgan with an object sets some options.

We set stream.write to a function that calls theAppLog.debug to use log4js with morgan to do the logging.

And then we call use with theHTTPLog to use the logger created by morgan to do the logging.

Conclusion

To use the Node.js Morgan logger, we can use it with log4js.