SFTP-Nodejs create folder in remote location - javascript

I had used the following node module for sftp implementation.
https://www.npmjs.com/package/ftps
My target is to create a folder in the remote location and save my file. In the see the available session( http://lftp.yar.ru/lftp-man.html) in the documentation it shows mkdir as an option. But I have no idea how to use it. Kindly help me create a folder in remote location. I am new to server development and this confuses me. Or is it possible to create a folder?

It seems to be in the documentation but not in the actual source code index.js
You will have to use another libary.

Just adding the following lines to index.js of node modules worked.
FTP.prototype.mkdir = function (directory , mode) {
return this.raw('mkdir '+mode+' '+directory)
};
The function call in the program is as follows
ftps.mkdir(path,['-p']).cd(path).addFile(file);

Related

How put API key on env files in a Node.js app?

this is my first time im trying to handle " Web api ", so i took this project to get many call but after it running well, when i try to click to "search" it dosent work,
I guess the problem arises from api call because chrome inspector show me that :
I was able to understand on the different forums, for handling apis call with Node.js that must be encapsulated API calls behind "Environment variable".
That the config.js file
When i try to put on the terminal export env.API_KEY='000000000000000' it made me :
export: not valid in this context: env.API_KEY
I hope you can point me in the right direction, I very tried everything, to run it that.
I personally like to use a npm package called dotenv:
You can install it by running npm i dotenv within your api directory.
Have a file called .env within your api directory which contains all of your environment variables:
APP_ID="000000000000000"
API_KEY="000000000000000"
Then change config.js to load all environment variable files when it is executed by including require('dotenv').config():
require('dotenv').config()
module.exports = {
APP_ID: process.env.APP_ID,
API_KEY: process.env.API_KEY,
BASE_URL: 'https://api.adzuna.com/v1/api/jobs',
BASE_PARAMS: 'search/1?&results_per_page=20&content-type=application/json',
};
Note: you will also want to add .env to your .gitingore so that your sensitive API keys aren't included in your git repository

Firebase functions in multiple separate folders

My source code looks like this:
API/index.ts (includes 1 firebase function trigger)
Extra/index.ts which includes
exports.ml_files = require("./ai-func");
exports.authHandler = require("./user-function");
exports.fileUploadListen = require("./fileupload.listener");
// exports.graphql = require("../API/src/index");
Currently my functions source directory directory is Extra. How do I make sure that the functions defined in API also get included in the final build? If I try to use exports.graphql the build breaks.
You must move all source code into the "functions" folder for deployment. The CLI will only deploy content from that folder, and from nowhere else. It's not possible to configure it otherwise - you must have a single folder where everything lives. As such, paths given to require() that look outside the folder (as you are doing now with ..) will simply not work.
I would recommend to try this concept of separation explained here.
We use it every of our Firebase projects and we are very happy with it :)

NodeJS - Cant find JSON File

I'm working on a express API and i want to connect to a mysql server with this api. Settings are stored in a settings.json file. I read this using
const config = JSON.parse(require(`fs`).readFileSync('../../settings.json'));
This works if the json file is in the same directory. But in this case, the settings file is in the base directory(./settings.json) but the file from where i want it to access is: ./modules/sql/mysql.js. It doesnt work:
Error: ENOENT: no such file or directory, open '../../settings.json'
Is there a better way to access/read a json file? or what am i doing wrong?
Btw. i dont want to pass the settings as a variable. I already tried it but if possible - i want to avoid it.
File/Directory strcture
did you not miss a ../ there?
try replace '../../settings.json' for '../../../settings.json'
btw, its better to use the __dirname approach, but it should work just fine without that too :)

Node Js Environment Specific Variables Dev/Testing/UAT

I am new to Node js, started developing the Angular application using Angular 1.2 and Node js. As of now, I have hardcoded the REST API(Java) endpoints in the node services.js. Now I want to load the base endpoint URI specific to the environment. I have tried few ways by setting a new key value for the process.env, a env file and load it. Can anyone please help me.
I have tried below approach.
Created devEnv.env file under root folder.
Added 3 key-value pairs
hostname = xyz
apikey = 123
devUrl = xyz/xyz/xyz.com/
Then in terminal, I am trying to add it to the source.
$ source denEnv.env
I am getting source not found.
Another way I have added the script in package.json file
{
"start-dev": "source devEnv.env; node server.js"
}
In terminal I executed
$ npm start-dev
It's also failing. Can anyone please let me know what mistake I am doing and what is the correct approach.
Thanks in advance.
There are three methods known to me:
1) .env file
You need to install dotenv package using npm install/yarn add and on top of your main file (e.g. index.js) put require('dotenv').config(). That should load your variables to node.
2) passed on a start
If you want pass a small amount of environmental variables you can try something like this in your package.json:
{
"start-dev": "hostname=xyz apikey=123 devUrl=xyz/xyz/xyz.com node server.js"
}
Advice: environmental variables should look like HOSTNAME, API_KEY or DEV_URL.
3) system environmental variables
Solution: Set environment variables from file
Your variables are most likely not being exported to the shell. To be able to source your devEnv.env script, try to modify it as follows:
#!/bin/bash
export hostname=xyz
export apikey=123
export devUrl=xyz/xyz/xyz.com/
You most likely need to give it executable rights:
chmod +x devEnv.env
And then source it by running:
. devEnv.env
Another example can be found here: Set environment variables from file

How to setup a destination path for a custom yeoman generator files?

I'm building a custom yeoman generator, so when it's time to create files they're created one directory above my current location, or at .., so for example if I run:
yo koala
From /home/diegoaguilar/koala the files will be created at /home/diegoaguilar. How am I supposed to tell the path where the generator should copy files? I really thought that would be process.cwd() or simply where yeoman generator is being ran from.
This is the code I got for files generation:
writing: {
app: function () {
this.fs.copyTpl(
this.templatePath('_package.json'),
this.destinationPath('package.json'),
{appname: this.appname}
);
this.fs.copy(
this.templatePath('_bower.json'),
this.destinationPath('bower.json')
);
},
projectfiles: function () {
this.fs.copy(
this.templatePath('editorconfig'),
this.destinationPath('.editorconfig')
);
this.fs.copy(
this.templatePath('jshintrc'),
this.destinationPath('.jshintrc')
);
}
},
First, I find it easier to use yeoman's this.template(), instead of using this.fs.copy()/this.fs.copyTpl() that are coming from the included instance of mem-fs-editor, but YMMV
Regardless, you need to set the this.sourceRoot('rel/path/to/source/root') and this.destinationRoot('rel/path/to/dest/root') in the generator before you try to write to make sure you have set the correct template and destination contexts. See yeoman's getting started guide on interacting with the files system from more information. The this.destinationRoot() should be defined relative to the root directory of your current project (I explain this below), while the this.sourceRoot() should be defined relative to the root directory of your generator's files.
You also have to consider that yeoman will try to figure out the root directory of whatever app you are currently in at the command line. It does this by navigating upwards (i.e. /home/diegoaguilar/koala -> /home/diegoaguilar/) until it finds a .yo-rc.json file. Yeoman then takes the directory of the nearest .yo-rc.json to where you ran the command as the intended root directory for your project.
In your case you may want to delete/move/rename /home/diegoaguilar/.yo-rc.json if it exists. Then you can create the directory you want your project to live inside and run the generator there. This would look like
/home/diegoaguilar/ $> mkdir koala
/home/diegoaguilar/ $> cd koala
/home/diegoaguilar/koala/ $> yo koala
If you want or need to leave your /home/diegoaguilar/.yo-rc.json there, you should set the this.destinationRoot() in the generator relative to /home/diegoaguilar/, so to write into /home/diegoaguilar/koala/ you would use this.destinationRoot('koala').

Categories

Resources