Categories
JavaScript Answers

How to bulk insert in MySQL using Node.js?

Sometimes, we want to bulk insert in MySQL using Node.js.

In this article, we’ll look at how to bulk insert in MySQL using Node.js.

How to bulk insert in MySQL using Node.js?

To bulk insert in MySQL using Node.js, we can use the connection’s query method.

For instance, we write

const mysql = require('mysql');
const conn = mysql.createConnection({
  //...
});

const sql = "INSERT INTO Test (name, email, n) VALUES ?";
const values = [
  ['demian', 'jane@gmail.com', 1],
  ['john', 'mary@gmail.com', 2],
  ['mark', 'alex@gmail.com', 3],
  ['pete', 'pete@gmail.com', 4]
];
conn.query(sql, [values], (err) => {
  if (err) throw err;
  conn.end();
});

to create the DB connection with mysql.createConnection.

Then we call conn.query with the sql and the values in a nested array to insert them all into the SQL.

The ? in the sql will be interpolated with the values to insert.

Then we call conn.end to end the DB connection once the data are inserted.

Conclusion

To bulk insert in MySQL using Node.js, we can use the connection’s query method.

Categories
JavaScript Answers

How to share variables between files in Node.js?

Sometimes, we want to share variables between files in Node.js.

In this article, we’ll look at how to share variables between files in Node.js.

How to share variables between files in Node.js?

To share variables between files in Node.js, we can put our shared variables in a module.

For instance, we write

module.js

const name = "foobar";

exports.name = name;

to export the name variable in module.js by assigning it to exports.name.

And then in main.js, we write

main.js

const myModule = require('./module');

const name = myModule.name;

to call require to include the module.js module and assign the exported contents to myModule.

And then we get the exports.name value with myModule.name,

Conclusion

To share variables between files in Node.js, we can put our shared variables in a module.

Categories
JavaScript Answers

How to read a text file using Node.js?

Sometimes, we want to read a text file using Node.js.

In this article, we’ll look at how to read a text file using Node.js.

How to read a text file using Node.js?

To read a text file using Node.js, we call fs.readFile.

For instance, we write

const fs = require('fs')

const filename = '/foo/bar.txt'

fs.readFile(filename, 'utf8', (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data)
})

to call fs.readFile with the filename, the encoding of the text file, and a callback that has the read file data stored in the `data parameter.

If there’s an error when reading the file, err is set.

Conclusion

To read a text file using Node.js, we call fs.readFile.

Categories
JavaScript Answers

How to limit concurrency when using ES6’s Promise.all()?

Sometimes, we want to limit concurrency when using ES6’s Promise.all().

In this article, we’ll look at how to limit concurrency when using ES6’s Promise.all().

How to limit concurrency when using ES6’s Promise.all()?

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

To install it, we run

npm i p-limit

For instance, we write

const pLimit = require('p-limit');

const limit = pLimit(3);

const urls = [
  "http://www.exampleone.com/",
  "http://www.exampletwo.com/",
  "http://www.examplethree.com/",
  "http://www.examplefour.com/",
]

const promises = urls.map(url => {
  return limit(() => fetchData(url));
});

(async () => {
  const result = await Promise.all(promises);
  console.log(result);
})();

to call pLimit with 3 to return the limit function that limits the number of concurrent promises to 3.

Then we call urls.map with a callback that returns the promise that’s returned by limit.

And then call Promise.all with promises to return the results from the promises.

Conclusion

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

Categories
JavaScript Answers

How to fix the “Error: Cannot find module ‘express'” error with Node.js?

Sometimes, we want to fix the "Error: Cannot find module ‘express’" error with Node.js.

In this article, we’ll look at how to fix the "Error: Cannot find module ‘express’" error with Node.js.

How to fix the "Error: Cannot find module ‘express’" error with Node.js?

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.

To install it, we run

npm install express

Conclusion

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.