Categories
JavaScript Answers

How to add connection pooling with Node.js and MySQL?

Sometimes, we want to add connection pooling with Node.js and MySQL.

In this article, we’ll look at how to add connection pooling with Node.js and MySQL.

How to add connection pooling with Node.js and MySQL?

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.

For instance, we write

const getConnection = (callback) => {
  pool.getConnection((err, connection) => {
    callback(err, connection);
  });
};

to define the getConnection function that takes the callback we want to call after the pool.getConnection is done.

We get the MySQL connection from the connection parameter.

And we call callback with the connection so we can use it elsewhere.

Then when we’re done, we call connection.release(); to release the connection.

Conclusion

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.

Categories
JavaScript Answers

How to auto generate migrations with Sequelize CLI from Sequelize models?

Sometimes, we want to auto generate migrations with Sequelize CLI from Sequelize models

In this article, we’ll look at how to auto generate migrations with Sequelize CLI from Sequelize models.

How to auto generate migrations with Sequelize CLI from Sequelize models?

To auto generate migrations with Sequelize CLI from Sequelize models, we can run sequelize migration:generate.

For instance, we run

sequelize migration:generate --name [name_of_migration]

to run sequelize migration:generate with the --name option followed by the name of the migration we want to create to create a blank migration file.

The model data has to be created manually.

Conclusion

To auto generate migrations with Sequelize CLI from Sequelize models, we can run sequelize migration:generate.

Categories
JavaScript Answers

How to get city name from a latitude and longitude point with Node.js?

Sometimes, we want to get city name from a latitude and longitude point with Node.js.

In this article, we’ll look at how to get city name from a latitude and longitude point with Node.js.

How to get city name from a latitude and longitude point with Node.js?

To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder package.

To install it, we run

npm i node-geocder

Then we use it by writing

const NodeGeocoder = require('node-geocoder');

const options = {
  provider: 'google',
  httpAdapter: 'https',
  apiKey: '...',
  formatter: 'json'
};

const geocoder = NodeGeocoder(options);

geocoder.reverse({
  lat: 49.5967439,
  lon: 77.3285038
}, (err, res) => {
  console.log(res);
});

We create a NodeGeocoder instance with some options to specify the map provider, API key, etc, and the format of the data with the formatter property.

Then we call geocoder.reverse with an object that has the lat latitude and lon longitude.

The callback’s res parameter has the location data for the given coordinates.

Conclusion

To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder package.

Categories
JavaScript Answers

How to upload images using Node.js, Express, and Mongoose?

Sometimes, we want to upload images using Node.js, Express, and Mongoose.

In this article, we’ll look at how to upload images using Node.js, Express, and Mongoose.

How to upload images using Node.js, Express, and Mongoose?

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

To use it, we write

app.use(express.bodyParser());

app.post('/todo/create', (req, res) => {
  res.send(console.dir(req.files));
});

to call express.bodyParser to return a middleware to parse multipart form data request bodies.

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

Then we create a POST route with app.post.

And we get the uploaded files from req.files in the route handler.

Then in the template, we write

form(action="/todo/create", method="POST", enctype="multipart/form-data")
    input(type='file', name='todo')
    button(type='submit') New

to add a form element with the action attribute set to /todo/create to submit our form data to the route we just created.

We set enctype to multipart/form-data to make it submit form data.

In the form element, we add a file input to let us accept files.

Conclusion

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

Categories
JavaScript Answers

How to make requests in sequential order Node.js?

Sometimes, we want to make requests in sequential order Node.js.

In this article, we’ll look at how to make requests in sequential order Node.js.

How to make requests in sequential order Node.js?

To make requests in sequential order Node.js, we can use axios.

To install it, we run

npm i axios

Then we write

const axios = require('axios')

const makeRequests = async () => {
  const {
    data
  } = await axios.get(url1)
  const {
    data: data2
  } = await axios.get(url2)
  const {
    data: data3
  } = await axios.get(url3)
}

to call axios.get to make GET requests to 3 different URLs url1, url2, and url3.

And we get the response from the data property of the resolved value of each promise.

await returns the resolve value of the promise.

Conclusion

To make requests in sequential order Node.js, we can use axios.