I have prepared the shell script to launch the node application (written using express framework). Shell script looks like below-
#!/bin/bash
cd <NODE.JS Project directory>
npm install --save
var1="connect_point1";
var2="connect_point2";
node app.js var1 var2
in app.js have included below lines-
//print process.argv
process.argv.for Each((val, index) => {console.log('${index}: ${val}');});
var cp1=var1;
var cp2=var2;
I am unable to get var1,var2 values which is passed from shell script to use it in node app.js script. Please suggest me correct way to achieve this.
In the log can see the variable var1 and var2 not defined. so issue is when i am trying to pass the shell script variable to node app.js script..pls suggest me how to pass those parameter correctly, so it can be used in ap.js and other subsequent node scripts.
Add a $ sign to your arguments inside bash script to indicate variables.
#!/bin/bash
var1="connect_point1"
var2="connect_point2"
node app.js $var1 $var2
app.js
process.argv.forEach((index, value) => console.log(index, value));
This is the output i see when running the above bash script:
./bash_script.sh
/usr/local/Cellar/node/12.4.0/bin/node 0
/Users/Username/Temp/app.js 1
connect_point1 2
connect_point2 3
here you can see that the argument values you need are located on index 2 and 3:
var cp1 = process.argv[2];
var cp2 = process.argv[3];
Related
I am trying to specify 2 arguments with express JS start command, as specified below:
npm start -x 5 -y 43
But in doing so, I can't pick the arguments with '-' and they are somehow skipped in the following code.
process.argv.forEach(function (val, index, array) {
log.info(index + ': ' + val);
log.error("========="+array[index].toString());
});
Please help me to get the arguments with '-' as initials?
Note: I have tried using Yarks, but its not working as specified in the manual.
To pass arguments in npm script you must use double dash --:
npm start -- -x 5 -y 43
Check npm-run-script command documentation for more details.
I was able to pass in arguments with simply
npm start myArg1 myArg2
Both bin/www and app.js could access them.
Is it possibille to run node.exe, pipe a text into it, and continue the interactive session?
I want to create a shortcut bat (or bash) file for editing my database.
Usually this is what I'm doing:
$ node
>var db=require('mydb')
>db.open('myserver')
>//Now I can start access the db
>db.query...
I want to do something like that:
$ node -i perDefinedDb.js
>db.query(.... //I don't want to define the DB each time I run the node.exe
I tried some like that:
echo console.log(a) | node.exe
This is the result:
3
And the program is Finish. I want to continue the node REPL after piping something into.
In Other Words:
I want to be able to use my DB from node REPL, without defining it each time.
Launch the REPL from your js file and you can give the context you want:
const repl = require('repl');
var db = require('mydb');
db.open('myserver');
repl.start('> ').context.db = db;
Now you just have to run this file (node myREPL.js) and you can REPL as usual.
Im trying to pass command line arguments to forever, that I want to parse in my app.
Currently I'm starting my app like this:
forever start -c "node --max-old-space-size=8192 --nouse-idle-notification" /home/ubuntu/node/server.js
I want to be able to read some arguments in my server.js.
My code for reading:
var arguments = process.argv.slice(2);
console.log(arguments[0]);
if(!arguments[0]) {
console.log("Error: Missing port.");
process.exit(1);
}
But how to I tell forever to pass arguments that can be read inside server.js?
According to the forever docs, you should use this format where you append your script arguments to the very end:
forever [action] [options] SCRIPT [script-options]
So for your example, you should be able to do this (omitting the -c options to make this a little more readable):
forever start server.js 9000 anotherarg evenanother
You can add the -c and the full path to server.js back into your real call, based upon your situation.
I have a bash script (Mac OS X) that in turns calls a Node.js command line application.
I normally call the Node.js app like this:
node mynodeapp events:"Open project"
Which node has no problem parsing as one parameter, in spite of the space between "Open" and "project".
I call my bash script like this:
. mybashscript.sh 2014-03-20 "Open project"
Inside the bash script I have:
EVENTSQUOTES=\"$2\"
echo node mixpanel-extract date:$1 events:$EVENTSQUOTES
node mixpanel-extract date:$1 events:$EVENTSQUOTES
Running the script produces:
node mixpanel-extract date:2014-03-20 events:"Open project"
Parameters: { date: '2014-03-20',
events: [ '"Open' ] }
So although the echo output line looks fine, the Parameters: output from my Node.js app tells me that bash splits the parameter in two. I've also tried wrapping it in more quotes e.g. EVENTSQUOTES='\"$2\"' but it makes no difference.
You need to use quote while calling also:
node mixpanel-extract date:"$1" events:"$EVENTSQUOTES"
echo node mixpanel-extract "date:$1" "events:$2"
node mixpanel-extract "date:$1" "events:$2"
You need to quote the variable when you use it as well, otherwise word splitting will occur.
I want the PHP equivalent of the solution given in assigning value to shell variable using a function return value from Python
In my php file, I read some constant values like this:-
$neededConstants = array("BASE_PATH","db_host","db_name","db_user","db_pass");
foreach($neededConstants as $each)
{
print constant($each);
}
And in my shell script I have this code so far:-
function getConfigVals()
{
php $PWD'/developer.php'
//How to collect the constant values here??
#echo "done - "$PWD'/admin_back/developer/developer.php'
}
cd ..
PROJECT_ROOT=$PWD
cd developer
# func1 parameters: a b
getConfigVals
I am able to execute the file through shell correctly.
To read further on what I am trying to do please check Cleanest way to read config settings from PHP file and upload entire project code using shell script
Updates
Corrected configs=getConfigVals replaced with getConfigVals
Solution
As answered by Fritschy, it works with this modification:-
PHP code -
function getConfigVals()
{
php $PWD'/developer.php'
#return $collected
#echo "done - "$PWD'/admin_back/developer/developer.php'
}
shell code -
result=$(getConfigVals)
echo $result
You have to execute the function and assign what is printed to that variable:
configs=$(getConfigVals)
See the manpage of that shell on expansion for more ;)