How do I use "stream.pipe(fs.createWriteStream())" - javascript

How do I use stream.pipe?
I'm running a function that outputs:
const fs = require('fs');
const screenshot = require('screenshot-stream');
const stream = screenshot('http://google.com', '1024x768');
stream.pipe(fs.createWriteStream('picture.png'));
My next step is taking that picture (picture.png) and assigning it here:
var content = fs.readFileSync('/path/to/img')
In other words, what is the path to the image so I can upload it?

Lets say, Your project directory name is "PROJECT", And you executed this file from the root directory of your project, Then the PICTURE.png file will be created in the root directory.
And the path to that file'll be __dirname + "/PICTURE.png" if you are still in the root directory of your project.

Related

Using Path Module in NODE.JS

I'm trying to store the absolute path of a file in a variable, exporting it through module.exports to use it in other files, for example, a file in another project folder, accessing it by require. After that, I use this variable as the path of the readFileSync method. Very well, when running the code in V.S Code, it works perfectly, but when I try to run it in the terminal, the path that appears is different! Why does it happen?
//path_module.js file code (located in 'path' folder):
const testPath = path.resolve('path','content', 'subfolder', 'test.txt');
console.log(testPath);
module.exports = testPath;
//fileSync_modules.js file code (located in 'fs' folder):
const fs = require('fs');
const testPath = require('../path/path_module')
const readFile = fs.readFileSync(testPath, 'utf8');
console.log(readFile);
When I run in VS. Code, the content of test.txt is visible. When I run in terminal, it gives a path error:
Error: ENOENT: no such file or directory, open 'C:\\Users\\home\\alvaro\\node\\fs\\path\\content\\subfolder\\test.txt'
As I'm running inside fs folder, it recognizes this folder as part of the path.
It looks like you're trying to make an path relative to the source file.
To do this, add __dirname to the start:
const testPath = path.resolve(__dirname, 'path','content', 'subfolder', 'test.txt');

How to access a group of files, regardless of folder name with fs/require

So I have a group of files organized like this:
./main/folder1/files.js
./main/differentfolder/files.js
./main/test/files.js
./main/test/files.js
The files are NOT named files.js, it's my way of saying that there's a lot of files in that folder.
How do I access all the files shown above without doing something like this:
const commandFiles = fs.readdirSync(`./main/folder1/files.js`)
const commandFiles2 = fs.readdirSync(`./main/differentfolder/files.js`)
const commandFiles3 = fs.readdirSync(`./main/test/files.js`)
//and so on
Just grab all the Javascript files inside "main" regardless of the folder name, both using "fs" and "require". I'd expect it to be the same. There is nothing but folders in ./main.
EDIT: I just want it so fs can check /main//files instead of what's inside of main
I easily solved this problem by using the require-all package which was something installed with the other packages I'm using for my app:
const fs = require('fs');
var folders = fs.readdirSync('./main/').filter(n => n !== '.DS_Store') //filter out that disgusting macOS file
folders.forEach(async (folder) => {
var cmdfolder = Object.values(require('require-all')(__dirname + `/main/${folder}`))
cmdfolder.forEach(async (key) => {
var file = require(`./main/${folder}/${key.fileName}`)'
//register file
})
})

Finding the directory portion of a String containing a file name in Node?

Processing a bunch of files using Node and I need to separate the file name from the directory. Does node have a simple way to do this without additional dependencies? I could use an NPM package like Filename Regex, but thought I'd check if there something available out of the box?
So for example suppose we have src/main/css/file.css. Hoping something like this is possible:
const fs = require('fs');
const path = fs.filePath(String pathAndFileName); //path = src/main/css
const file = fs.fileName(String pathAndFileName); //file = file.css
The utilities for manipulating file paths are in the path module.
https://nodejs.org/api/path.html
const {dirname, basename} = require('path');
const path = dirname(String pathAndFileName);
const file = basename(String pathAndFileName);

process.env.PWD vs process.cwd()

I am using Meteor JS...and within my Meteor app I am using node to query the contents of different directories within the app....
When I use process.env.PWD to query the contents of a folder I get a different result from when I use process.cwd() to query the results of a folder.
var dirServer = process.env.PWD + '/server/';
var dirServerFiles = fs.readdirSync(dirServer);
console.log(dirServerFiles);
//outputs: [ 'ephe', 'fixstars.cat', 'sepl_30.se1', 'server.js' ]
vs
var serverFolderFilesDir = process.cwd() +"/app/server";
var serverFolderFiles = fs.readdirSync(serverFolderFilesDir);
console.log(serverFolderFiles);
//outputs: [ 'server.js' ]
using process.cwd() only shows server.js within the Meteor.
Why is this?
How is process.cwd() different from process.env.PWD?
They're related but not the same thing.
process.env.PWD is the working directory when the process was started. This stays the same for the entire process.
process.cwd() is the current working directory. It reflects changes made via process.chdir().
It's possible to manipulate PWD but doing so would be meaningless, that variable isn't used by anything, it's just there for convenience.
For computing paths you probably want to do it this way:
var path = require('path');
path.resolve(__dirname, 'app/server')
Where __dirname reflects the directory the source file this code is defined in resides. It's wrong to expect that cwd() will be anywhere near that. If your server process is launched from anywhere but the main source directory all your paths will be incorrect using cwd().

How to get document root in sailsjs?

To read a file located in the document root of the sails project I currently use an absolute path.
/var/www/project/file.conf
How can I get the document root path in sails? Or how can I read the file in a controller by using relative path?
in Sails the root path is: sails.config.appPath
Since the main application file is app.js in the root folder, process.cwd() should work just fine for you.
To extend what #Maciej Maczor Kaczorowski posted...
var path = require('path').resolve(sails.config.appPath, 'file.conf');
or
var path = require('path').resolve(".", 'file.conf');
or
var path = require('path').resolve('file.conf');
or
var path = sails.config.appPath + 'file.conf';
or
var path = res.baseUrl + 'file.conf';

Categories

Resources