Categories
JavaScript Answers

How to programmatically shut down an instance of Express.js?

Sometimes, we want to programmatically shut down an instance of Express.js.

In this article, we’ll look at how to programmatically shut down an instance of Express.js.

How to programmatically shut down an instance of Express.js?

To programmatically shut down an instance of Express.js, we can call the close method.

For instance, we write

const server = app.listen(3000);

const handler = () => {
  server.close();
};

to call the app.listen with the port number to return an Express server object.

Then we call server.close to shut down the Express instance in the handler function.

Conclusion

To programmatically shut down an instance of Express.js, we can call the close method.

Categories
JavaScript Answers

How to pipe a stream to s3.upload() with Node.js?

Sometimes, we want to pipe a stream to s3.upload() with Node.js.

In this article, we’ll look at how to pipe a stream to s3.upload() with Node.js.

How to pipe a stream to s3.upload() with Node.js?

To pipe a stream to s3.upload() with Node.js, we can use the s3.upload method with the stream.PassThrough method.

For instance, we write

const uploadFromStream = (s3) => {
  const pass = new stream.PassThrough();

  const params = {
    Bucket: BUCKET,
    Key: KEY,
    Body: pass
  };
  s3.upload(params, (err, data) => {
    console.log(err, data);
  });

  return pass;
}

inputStream
  .pipe(uploadFromStream(s3));

to create the uploadFromStream function that calls the stream.PassThrough method to return an object that we can use as the value of the Body property.

And then we call s3.upload with params that has the Bucket, Key, and Body.

Finally, we call inputStream.pipe with uploadFromStream(s3) to pipe the stream to the s3.upload method.

Conclusion

To pipe a stream to s3.upload() with Node.js, we can use the s3.upload method with the stream.PassThrough method.

Categories
JavaScript Answers

How to convert entity to plain object with Sequelize?

Sometimes, we want to convert entity to plain object with Sequelize.

In this article, we’ll look at how to convert entity to plain object with Sequelize.

How to convert entity to plain object with Sequelize?

To convert entity to plain object with Sequelize, we can call findAll with raw and nest set to true.

For instance, we write

db.Sensors.findAll({
  where: {
    nodeid
  },
  raw: true,
  nest: true,
})

to call findAll with an object with a where property to find the item with the given nodeid value.

And we set raw to true to return the result as a plain object.

We set nest to true to prevent nesting of associations.

Conclusion

To convert entity to plain object with Sequelize, we can call findAll with raw and nest set to true.

Categories
JavaScript Answers

How to add a client for a socket.io server with Node.js?

Sometimes, we want to add a client for a socket.io server with Node.js.

In this article, we’ll look at how to add a client for a socket.io server with Node.js.

How to add a client for a socket.io server with Node.js?

To add a client for a socket.io server with Node.js, we can use the socket.io-client package.

To install, it we run

npm i socket.io-client

Then use the package by writing

const io = require('socket.io-client');
const socket = io.connect('http://localhost:3000', {
  reconnect: true
});

socket.on('connect', (socket) => {
  console.log('Connected!');
});
socket.emit('ch01', 'me', 'test msg');

We connect by calling io.connect with the server ID address.

Then we call socket.on with 'connect' to listen for connection success.

And then we call socket.emit to send a message to a channel.

Next, we add our server by writing

const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);

io.on('connection', (socket) => {
  console.log('connection');

  socket.on('ch01', (from, msg) => {
    console.log('message', from, ' saying ', msg);
  });

});

http.listen(3000, () => {
  console.log('listening on *:3000');
});

to call io.on with 'connection' to listen for client connections.

And then we call socket.one with 'ch01' to listen for messages on room 'ch01'.

Conclusion

To add a client for a socket.io server with Node.js, we can use the socket.io-client package.

Categories
JavaScript Answers

How to make connection to Postgres via Node.js?

Sometimes, we want to make connection to Postgres via Node.js.

In this article, we’ll look at how to make connection to Postgres via Node.js.

How to make connection to Postgres via Node.js?

To make connection to Postgres via Node.js, we can use the pg module.

To install it, we run

npm i pg

Then we use it by writing

const {
  Pool
} = require('pg');

const config = {
  user: 'foo',
  database: 'my_db',
  password: 'secret',
  host: 'localhost',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
};

const pool = new Pool(config);
pool.on('error', (err, client) => {
  console.error('idle client error', err.message, err.stack);
});
pool.query('SELECT $1::int AS number', ['2'], (err, res) => {
  if (err) {
    return console.error('error running query', err);
  }
  console.log('number:', res.rows[0].number);
});

to create a new Pool instance with the config object that has the database host, name, port, and credentials.

Then we call pool.on with 'error' to catch any errors with the connection.

And then we call pool.query to make a database query with the parameterized SQL string, values, and a callback that has the result of the query.

Conclusion

To make connection to Postgres via Node.js, we can use the pg module.