Passing arguments in Express JS - node.js

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.

Related

how to pass shell script variable to node app.js?

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];

how to pass float value as query params using curl?

I have a python flask server running and the following http GET works on my browser, http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741 , however when I use command curl http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741 it doesn't work. How to correct my query to work for curl?
You have to pass URL as a string.
It's because for shell a lot of characters used in URL have special meaning (ie = is an assignment, & makes command to run in background, etc). So
curl "http://example.com/v1/api?lng=18.565810740668912&lat=-33.93153605161741"
should work as expected.
To pass the query-parameters separated and let cURL build the query-string we can also use option -G together with usual -d like
curl -G -d 'lng=18.565810740668912' -d 'lat=-33.93153605161741' http://example.com/v1/api
See also: Construct a Query String (TLDR: Use -G argument)

Why does npm start ignore escaped backticks from argument?

I have a super simple index.js file that outputs the first argument it receives:
// Args 0 & 1 are /usr/bin/node and path to file
console.log(process.argv[2]);
When I run the following:
node index.js test``backticks
the output is testbackticks, and as expected, the backticks are ignored.
Calling this again, with escaping this time:
node index.js test\`\`backticks
the output is test``backticks.
My package.json has a start script like so:
"start": "node ./index.js"
Calling npm start -- test``backticks will result in them being ignored, just like with the node.js call.
However, calling this with escaping:
npm start -- test\`\`backticks
Also results in the same output, which is simply:
testbackticks
With the backticks ignored as well.
Escaping npm start with double or single quotes worked fine, it is just the backticks that are ignored.
Why does calling npm start and node produces different results and how come backticks can't be escaped in npm start ?
Lastly, how can I, if possible, pass a string with backticks as an argument to npm start ?

how to escape whitespace in Node-RED exec node command

Situation:
In my Node-RED flow there is a call to another program where I use the exec node.
The arguments are set-up in a function node and passed as the msg.payload to the exec node.
This works fine and returns the expected results as long as the command has no space in it.
Problem:
A customer cannot use the call to the command on his system, because the path tp the installation of the program contains a whitespace.
What I tried and didn't work:
Escaping the whitespace with ^ as read in another question: /opt/path^ toProgram^ directory/program
Escaping the whitespace with \\: /opt/path\\ toProgram\\ directory/program
Quoting the whole string with "": "/opt/path toProgram directory/program"
Quoting the whole string and escaping the whitespace: "/opt/path\\ toProgram\\ directory/program"
Quoting the whole string with '': '/opt/path toProgram directory/program'
Leaving the command line empty/""and combining any of the above points with its arguments to one string (in the function node that sets up the arguments for that exec node) and passing it on as the msg.payload -> input parameters in the exec node config panel. No Success.
What's not the problem:
The program itself works fine, on mine and on the customers system, it's only the path that is different
Other than that specific command string with whitespace, the configuration of the exec node and its msg.payload ( = input parameters or arguments) as well as the "use spawn() instead of exec()?" is fine and works
Request
Is there any other way to escape the whitespace that I'm not aware of so the path to the program can be found? From my understanding the whitespace is interpreted as a separator for the input arguments, which it should be, but not on the command string.
This should be a quick fix in my opinion, however nothing seems to work that usually works in node, js, php or bash.. thanks in advance for any hints and ideas!
Environment:
Node-RED version: v0.15.2 |
Node.js version: v5.12.0 |
Mac OS X 10.11.6 = Darwin 15.6.0 x64 LE
Screenshots
This is the part of the flow:
This config works:
This config does not work:
I've just tested this and option 3 (Quoting the path with "") works fine.
Putting "/home/foo/foo bar/date" in the command section of the exec node executed the binary correctly
After a good nights sleep the solution (at least what I thought at the time) was to adapt the exec node in node-red itself: The original code appended the additional arguments (the ones separated by whitespace) to the command, and then slice the whole string by whitespace into an array (node.cmd is the path to the command with or without whitespace):
before: var arg = node.cmd; arg += " "+msg.payload; arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
and the (earlier, incomplete) solution was to prepend the command with the builtin array.unshift(<value>) function to the array after the slice operation, so that the command path with the whitespace is not affected by the array creation:
after: var arg = ""; ...add msg.payload and slice arg string into array.. arg.unshift(node.cmd);
The file can be found at: nodered node 75-exec.js on github, in the lines 46-51
Update to real solution:
After further investigation it was clear, that the Use spawn() instead of exec()? option was the problem: the command part with whitespace in double quotes and using exec() worked fine. And my approach couldn't be used when the command path contained an argument, e.g. in /home/foo bar/date -r 1000.
Following the issue at the official github repository of node-red the command part to the arguments array at the beginning after the slice but instead to have the command path with whitespace in quotes and later unwrapping them, copying the behaviour when exec() is used. He added this line in the original file after line 51: if (/^".*"$/.test(cmd)) { cmd = cmd.slice(1,-1); } and now both spawn() and exec() work the same - credits to him, unfortunately I don't know his username here.

NodeJS passing command line arguments to forever

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.

Resources