Categories
JavaScript Answers

How to find the size of the file in Node.js?

Sometimes, we want to find the size of the file in Node.js.

In this article, we’ll look at how to find the size of the file in Node.js.

How to find the size of the file in Node.js?

To find the size of the file in Node.js, we can use the fs.statSync method.

For instance, we write

const fs = require("fs");
const stats = fs.statSync("myfile.txt")
const fileSizeInBytes = stats.size;
const fileSizeInMegabytes = fileSizeInBytes / (1024 ** 2);

to call fs.statSync with the path of the file that we want to get the size of.

Then we get the file size in bytes with stats.size.

Next, we convert the size to megabytes with fileSizeInBytes / (1024 ** 2).

Conclusion

To find the size of the file in Node.js, we can use the fs.statSync method.

Categories
JavaScript Answers

How to invoke an AWS Lambda function from within another lambda function with Node.js?

Sometimes, we want to invoke an AWS Lambda function from within another lambda function with Node.js.

In this article, we’ll look at how to invoke an AWS Lambda function from within another lambda function with Node.js.

How to invoke an AWS Lambda function from within another lambda function with Node.js?

To invoke an AWS Lambda function from within another lambda function with Node.js, we can call the lambda.invoke method.

For instance, we write

const AWS = require('aws-sdk');
AWS.config.region = 'eu-west-1';
const lambda = new AWS.Lambda();

exports.handler = (event, context) => {
  const params = {
    FunctionName: 'LambdaB',
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: '{ "name" : "Jane" }'
  };

  lambda.invoke(params, (err, data) => {
    if (err) {
      context.fail(err);
    } else {
      context.succeed('Lambda_B said ' + data.Payload);
    }
  })
}

in the first lambda to define a unction that calls lambda.invoke with the params object.

In params, we specify the name of the Lambda function to run with FunctionName.

And we set the Payload that we pass to 'LambdaB'.

Then in LambdaB, we listen for invocations with

exports.handler = (event, context) => {
  context.succeed(event.name);
};

And get the Payload value from event.

Conclusion

To invoke an AWS Lambda function from within another lambda function with Node.js, we can call the lambda.invoke method.

Categories
JavaScript Answers

How to prevent SQL injection in Node.js?

Sometimes, we want to prevent SQL injection in Node.js.

In this article, we’ll look at how to prevent SQL injection in Node.js.

How to prevent SQL injection in Node.js?

To prevent SQL injection in Node.js, we should be using parameterized queries.

For instance, we write

const userId = 5;

const query = connection.query('SELECT * FROM users WHERE id = ?', [userId], (err, results) => {
  //...
});

to call the connection.query method from the node-mysql-native package.

We call it with a parameterized select statement.

We have id = ? and pass in an array with the array of values to replace each ? in the SQL string to escape the values we pass in to replace the ?.

Therefore, userId is escaped before it’s interpolated and the query is run.

Then from the results parameter of the callback, we get the query result.

Conclusion

To prevent SQL injection in Node.js, we should be using parameterized queries.

Categories
JavaScript Answers

How to properly close Node Express server?

Sometimes, we we want to properly close Node Express server.

In this article, we’ll look at how to properly close Node Express server.

How to properly close Node Express server?

To properly close Node Express server, we can call the close method.

For instance, we write

const server = app.listen(3000);

//...

server.close((err) => {
  console.log('server closed')
  process.exit(err ? 1 : 0)
})

to call server.close with a callback that has the error err parameter.

If err is set, then there’s an error when closing the Express server and we call process.exit with exit code 1.

Otherwise, we call process.exit with exit code 0.

Conclusion

To properly close Node Express server, we can call the close method.

Categories
JavaScript Answers

How to execute a “WHERE col IN ()” query with node-postgres?

Sometimes, we want to execute a "WHERE col IN ()" query with node-postgres.

In this article, we’ll look at how to execute a "WHERE col IN ()" query with node-postgres.

How to execute a "WHERE col IN ()" query with node-postgres?

To execute a "WHERE col IN ()" query with node-postgres, we can use the client.query method.

For instance, we write

const ids = [1, 3, 4];

const q = client.query('SELECT Id FROM MyTable WHERE Id = ANY($1::int[])', [ids]);

q.on('row', (row) => {
  console.log(row);
})

to call client.query with the parameterized SQL string, and an array of values we set as the parameter values.

We add ids as the value of the value of ANY($1::int[]).

Then we call q.on with 'row' to return the query results and get the result from the row parameter.

Conclusion

To execute a "WHERE col IN ()" query with node-postgres, we can use the client.query method.