# Install NVM (Node - Js Version Manager)
# Install NVM (Node - Js Version Manager)
js
Cheat Sheet
presented by
server.listen(3000);
const fs = require('fs');
const https = require('https');
const path = require('path');
server.listen(3000);
(async () => {
const hosts = await fs.readFile(
'/etc/hosts',
{ encoding: 'utf8' }
);
console.log(hosts);
await fs.writeFile(
path.join(__dirname, 'localCopy.txt'),
hosts,
{ encoding: 'utf8' }
);
})();
// ...
});
Provide an HTTP API with Express
On CLI:
$ npm install body-parser cors express
In Node.js:
'use strict';
app.use(cors());
app.use(bodyParser.json());
server.listen(3000);
Set up Logging
On CLI:
$ npm install flaschenpost
In Node.js:
'use strict';
server.listen(port, () => {
logger.info('Server started ...', { port });
});
In Node.js:
'use strict';
server.listen(port);
Compute Hash
'use strict';
console.log(hash);
Encrypt Text
'use strict';
const iv = crypto.randomBytes(16);
console.log(cipherText);
Decrypt Text
Please note: key, iv und cipherText need to be the same as
those that were used for encryption.
const decryptionAlgorithm = crypto.createDecipheriv('aes-256-cbc',
key, iv);
console.log(resultingText);
Create an UUID
Requires Node.js 15.6.0 or later
'use strict';
console.log({
host: { architecture: arch, platform },
node: { version },
process: { id: pid, uptime },
cpuUsage: { user: userCPUTime, system: systemCPUTime },
memoryUsage: { rss, maxRss: maxRSS, heapTotal, heapUsed,
external },
diskUsage: { read: fsRead, write: fsWrite }
});
Set up Mocha
Install on CLI:
$ npm install assertthat mocha
In Node.js:
'use strict';
suite('add', () => {
test('returns the sum of two numbers.', async () => {
const left = 23;
const right = 42;
assert.that(actual).is.equalTo(expected);
});
});
Call on CLI:
$ npx mocha --async-only --bail --recursive --ui tdd
Configure ESLint
On CLI:
$ npm install eslint eslint-config-es
In .eslintrc.json:
{
"extends": "es/node"
}
Configure TypeScript
On CLI:
$ npm install typescript ts-node
In tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
© Oleksii Lishchyshyn/Shutterstock.com, © Bill81/Shutterstock.com
© Josep.Ng/Shutterstock.com, © Darko Petrovic/Shutterstock.com,
"esModuleInterop": true,
"lib": [ "esnext" ],
"module": "commonjs",
"outDir": "build",
"resolveJsonModule": true,
"strict": true,
"target": "es2019"
},
"include": [
"./**/*.ts"
]
}
javascript-conference.com