Node.js development
1. Introduction
Sample app references, eg: cou ulises
3. Multiple versions of Node.js on Linux
How To Run Multiple Versions of Node.js on Linux (nvm)
https://medium.com/better-programming/how-to-run-multiple-versions-of-node-js-on-linux-c3833f9c9234
Warning: node -v reports the selected version while nodejs -v seems to output the OS default one.
Installing and using
https://github.com/nvm-sh/nvm#installation-and-update
Eg:
$ nvm install 14
Downloading and installing node v14.21.3...
Downloading https://nodejs.org/dist/v14.21.3/node-v14.21.3-linux-x64.tar.xz...
##################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.21.3 (npm v6.14.18)
$ node -v
v14.21.3
$ nvm use 14
Now using node v14.21.3 (npm v6.14.18)
13. Define required Node.js and NPM versions
The .npmrc is a configuration file for NPM and it defines the settings on how NPM should behave when running commands.
The following sampe files enforce Node.js 18 and NPM 9 (eg when running 'yarn install') the he following error appear in the console:
$ yarn install
yarn install v1.22.17
[1/5] Validating package.json...
error myapp_back@22.2.24-SNAPSHOT: The engine "node" is incompatible with this module. Expected version "^18.18.0". Got "10.23.0"
package.json
{
"engines": {
"npm": "^9.8.1",
"node": "^18.18.0"
},
"name": "myapp_back",
"license": "UNLICENSED",
"version": "22.3.44-SNAPSHOT",
"description": "",
"dependencies": {
"aws-sdk": "^2.1038.0",
"dotenv": "^10.0.0",
"node-fetch": "2",
"serverless-http": "^2.7.0"
},
"devDependencies": {
"prettier": "^2.5.0",
"serverless-dynamodb-local": "^0.2.40",
"serverless-offline": "^8.3.1",
"serverless-offline-ses": "^0.0.7",
"serverless-offline-sqs": "^6.0.0",
"serverless-s3-local": "^0.6.20"
}
}
.npmrc
engine-strict=true
14. Syntax
14.1. const, let & var
const - declares a constant
let - declares a block-scope variable
var - declares a function-scope variable
14.2. async/await (instead of promises or callbacks)
Error handling with async/await and promises, n² ways to shoot yourself in the foot
https://catchjs.com/Docs/AsyncAwait
14.3. Log
console.log('%s failed to login %i times', user, attempts);
console.log(JSON.stringify(myObject, null, 2));
20. AWS Lambda w/ Node.js
Building Lambda functions with Node.js
https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html
20.1. How to read artifact version at runtime?
package.json
{
"name": "myapp_back",
"version": "22.2.10",
...
}
src/functions/myLambda/handler.js
...
const myLambda = async (event, context, callback) => {
var tech_comp_version = require('../../../package.json').version;
console.info("======== Artifact version: %s ========", tech_comp_version);
...
Note: The path to file package.json must be relative to the directory location of the function
21. Callback (w/ log)
const response_success = {
statusCode: 200,
body: JSON.stringify({
message: 'ok'
}),
};
const response_error = {
statusCode: 400,
body: JSON.stringify({
message: 'error'
}),
};
if (error) {
console.log('END gSuiteCallback:\n%s', JSON.stringify(response_error, null, 2));
return callback(response_error)
} else {
console.log('END gSuiteCallback:\n%s', JSON.stringify(response_success, null, 2));
callback(undefined, response_success)
}