Categories
JavaScript Answers

How to parse huge log files and read it in line-by-line Node.js?

Sometimes, we want to parse huge log files and read it in line-by-line Node.js.

In this article, we’ll look at how to parse huge log files and read it in line-by-line Node.js.

How to parse huge log files and read it in line-by-line Node.js?

To parse huge log files and read it in line-by-line Node.js, we can use the event-stream package.

To install it, we run

npm install event-stream

Then we use it by writing

const fs = require('fs')
const es = require('event-stream');

let lineNr = 0;

const s = fs.createReadStream('very-large-file.csv')
  .pipe(es.split())
  .pipe(es.mapSync((line) => {
      s.pause();
      lineNr += 1;
      logMemoryUsage(lineNr);
      s.resume();
    })
    .on('error', (err) => {
      console.log('Error while reading file.', err);
    })
    .on('end', () => {
      console.log('Read entire file.')
    })
  );

to call fs.createReadStream with the file we want to read.

And we call pipe with es.split to split the lines from the files.

And then we call es.mapSync with a callback that calls s.pause to pause read the file.

And then we run the code we want to with line read from the file.

Then we call s.resume to resume reading the file.

Also, we call on with 'error' and 'end' to listen for those events.

'error' event is emitted when there’s an error reading the file.

'end' event is emitted when the file reading operation is done.

Conclusion

To parse huge log files and read it in line-by-line Node.js, we can use the event-stream package.

Categories
JavaScript Answers

How to set up a SSL certificate for an Express.js server?

Sometimes, we want to set up a SSL certificate for an Express.js server.

In this article, we’ll look at how to set up a SSL certificate for an Express.js server.

How to set up a SSL certificate for an Express.js server?

To set up a SSL certificate for an Express.js server, we can http.createServer with the certificate and private key files.

For instance, we write

const privateKey = fs.readFileSync('privatekey.pem');
const certificate = fs.readFileSync('certificate.pem');

https.createServer({
  key: privateKey,
  cert: certificate
}, app).listen(port);

to call https.createServer with the key and cert properties set to the privateKey and certificate files respectively.

We read the files synchronously with readFileSync.

Then we call listen with the port that we use to listen for requests.

Conclusion

To set up a SSL certificate for an Express.js server, we can http.createServer with the certificate and private key files.

Categories
JavaScript Answers

How to get binary content in Node.js using request?

Sometimes, we want to get binary content in Node.js using request.

In this article, we’ll look at how to get binary content in Node.js using request.

How to get binary content in Node.js using request?

To get binary content in Node.js using request, we should set the encoding setting to null.

For instance, we write

const requestSettings = {
  method: 'GET',
  url,
  encoding: null
};

request(requestSettings, (error, response, body) => {
  // ...
})

to call request with requestSettings and a callback that’s run when the request is done.

We set encoding to null so that the response body in the callback would give us the binary data from the response.

Conclusion

To get binary content in Node.js using request, we should set the encoding setting to null.

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.