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.

Categories
JavaScript Answers

How to use the DOMParser with Node.js?

Sometimes, we want to use the DOMParser with Node.js.

In this article, we’ll look at how to use the DOMParser with Node.js.

How to use the DOMParser with Node.js?

To use the DOMParser with Node.js, we can use the Cheerio library.

We install it by running

npm i cheerio

Then, we write

const cheerio = require('cheerio');

const $ = cheerio.load(`<!DOCTYPE html><p>Hello world</p>`);
$('p').text('Bye moon');
$.html();

to call cheerio.load with an HTML string to load the HTML string into the $ DOM object.

Then we select p elements and call text to set the text of the p element.

And we call $.html to return the HTML of the manipulate document.

Conclusion

To use the DOMParser with Node.js, we can use the Cheerio library.