joplin-notes/joplin notion extension/Web Development/nodejs/Extras.md
2026-11-04 00:33:23 +05:30

175 lines
6.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Extras
- https://stackoverflow.com/questions/36022926/what-do-the-save-flags-do-with-npm-install
`npm i <package_name> --save` and `npm i <package_name> ` are same now and they also update dependencies of package too. to not update dependencies of package use `npm i <package_name> --no-save`
- nvm(Node version manager)
`curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash`
 
`nvm ls-remote` : for getting list of available versions which can be downloaded
`nvm list` : for getting list of downloaded versions of node
`nvm install 14.0.0` : for downloading and installing version 14.0.0
`nvm use 14.0.0` : switching to version 14.0.0
`nvm use system`: switching to version when nodejs first installed
`nvm alias default 18.0`: this will make 18 as default
- https://github.com/ishtms/learn-nodejs-hard-way (without installing other frameworks)
- https://youtu.be/Nh-252u80F4 : get wifi password
- Running nodejs in background
https://sentrytwo.com/92df06609b
- To avoid running sudo
Why You Should Never Run "sudo npm"... https://www.youtube.com/watch?v=aFrR56sqC_A
- **pm2**
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
install this npm package globally or locally.
`npm i -g pm2`
Using this we can start multiple node instances with single command. But first starting node application instances in background
`pm2 start app.js --name app1` same as `node app.js` but in background
to stop node instance use `pm2 stop app1`
multiple node instance in file `ecosystem.config.js`
```js
module.exports = {
apps: [
{
name: 'main',
script: 'index.js',
watch: true,
env: {
NODE_ENV: 'production',
PORT: 8081
}
},
{
name: 'ytdownloadspace',
script: '../ytDownloadSpace/app.js',
watch: true,
env: {
NODE_ENV: 'production',
PORT: 8080
}
},
{
name: 'ytdlApi',
script: '../ytdlApi/app.js',
watch: true,
env: {
NODE_ENV: 'production',
PORT: 8083
}
}
]
};
```
and fiannly when use
`pm2 start ecosystem-config.js`.
To stop all these started with this file use
`pm2 stop ecosystem-config.js`
To list processes use
`pm2 list`
- installing packages through repo link
`npm i https://github.com/expressjs/express`
https://www.pluralsight.com/resources/blog/guides/install-npm-packages-from-gitgithub
&nbsp;
- download a file with https and fs module
```js
const https = require("https"); // Use 'http' for non-HTTPS links
const fs = require("fs");
const fileUrl =
"https://cdn.discordapp.com/attachments/1473231612341915668/1473231989501853819/packages_sorted2.txt?ex=699575b5&is=69942435&hm=66884969a27901525409ff1e5400619ea5305667a130476b7982e041c55297e8&"; // Replace with your file URL
const destinationPath = "./downloaded-file.txt"; // Replace with desired local path and filename
const file = fs.createWriteStream(destinationPath);
https
.get(fileUrl, (response) => {
// Check if the request was successful (status code 200)
if (response.statusCode !== 200) {
console.error(`Download failed, status code: ${response.statusCode}`);
file.close(); // Close the write stream
fs.unlink(destinationPath, () => {}); // Delete the partial file
return;
}
// Pipe the response stream to the file stream
response.pipe(file);
// Event listener for when the download is complete
file.on("finish", () => {
file.close();
console.log("Download Completed!");
});
})
.on("error", (err) => {
// Handle network errors
fs.unlink(destinationPath, () => {}); // Delete the partial file on error
console.error("Error during download:", err.message);
});
```
&nbsp;
tips
1. to prevent adding of installed packages in package.json use `--no-save` flag when installing packages. eg: `npm i --no-save sqlite3`
&nbsp;
&nbsp;
better-sqlite guide
https://www.w3resource.com/sqlite/snippets/better-sqlite3.php
[Comprehensive Guide to Better-SQLite3 (16_5_2026 5:48:16 am).html](../../../_resources/Comprehensive%20Guide%20to%20Better-SQLite3%20%2816_5_2026%205.html)
for parameterized query, string concatenation using `||`
```js
selectQuery = `select * from ${songDataTableEnums.tablename}
where ${songDataTableEnums.columns.songname} like ? || '%' or ${songDataTableEnums.columns.songname} like '% '|| ? || '%'
or ${songDataTableEnums.columns.artist_name} like ? || '%' or ${songDataTableEnums.columns.artist_name} like '% '|| ? || '%'`;
```
&nbsp;
generate thumbnails of video
```js
const ffmpeg = require('fluent-ffmpeg');
ffmpeg('path/to/video.mp4')
.screenshots({
count: 1,
timemarks: ['00:00:02'], // Capture at 2 seconds
filename: 'thumbnail.png',
folder: 'output/path/'
});
```
or
```js
ffmpeg('input.mp4')
.screenshots({
count: 3, // Number of screenshots to take
timestamps: ['10%', '30sec', 60], // Locations: percentages, strings, or numbers (seconds)
filename: 'thumbnail-at-%s-seconds.png', // %s inserts the timestamp
folder: './output', // Destination directory
size: '320x240' // Rescales output width x height
})
.on('end', () => {
console.log('Screenshots taken successfully!');
})
.on('error', (err) => {
console.error('An error occurred: ' + err.message);
});
```
* count: Total number of images to generate. If timestamps is not provided, it automatically distributes screenshots evenly across the video duration.
* timestamps: An array specifying exact positions to snapshot.
* Numbers represent absolute seconds (e.g., 15).
* Strings format time as HH:MM:SS.milli or specific counts (e.g., '00:01:20').
* Percentages evaluate relative to video length (e.g., '50%').
* folder: Path to the directory where files will save.
* filename: Format for file naming. Use %s to interpolate the specific frame's timestamp or %i for the index sequence.