How to run node.js application from script? - javascript

I am trying to run my node.js application from a script I have written:
echo "Starting node application"
sudo node /home/pi/PPBot/bot.js
exit 0
I run the script like this: sudo /etc/init.d/botscript
The output when running the script is:
Start node application
sudo: node: command not found
I have also tried replacing node by /home/pi/.nvm/versions/node/v.8.11.3/bin/node but this resulted in the same output.
I have already installed NodeJS through NVM. Simply using the command node bot.js works from the command line. However as can be seen above it does not work through the script.

if you want to run a simple node js file then use the command
-> node filename
ex.: node server.js
if you want to run a node js file with nodemon then use the command
-> nodemon filename
ex.: nodemon server.js

Related

Http-server running in background with nohup doesn't remain live

I have an ubuntu server. On which I have transferred some files which have some js and html code.
I used http-server from node.js to start a web server so that I can display the html page on the server.
I used nohup so that it can remain running even if I disconnect or close my system.
Here is the command I used:
nohup http-server -p 8000 -a 10.4.145.182 &
Now this helps me visualize the files on 10.4.145.182:8000 but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.
I thought nohup helps run things in background even if one closes their system or logs out of server.
How do I make this web server running always then and accessible to url to everyone
Thanks
EDIT:
As per the suggestion below of using pm2, I installed latest version of node and then started service with pm2.
It says service started for http-server but when I go the to ip with port 8000 on browser it doesn't open up.
Here is the command I ran in my directory which has the html and d3 files.
$ pm2 start $(which http-server) -p 8000
And here is the output in shell of the pm2
Try this:
http-server & exit
You can also specify a port number:
http-server -p 8082 & exit
Reference here
I suggest you use pm2 . Long story short , check the official link https://www.npmjs.com/package/pm2
Steps :
Install :
npm install pm2 -g
Run your app :
pm2 start app.js
In your case use :
pm2 start /usr/local/bin/http-server -- -p 8080
for reboot to work run :
pm2 startup systemd
take the last line , change the user and home path and run the modified line as SUDO !!! you need sudo access for this !!!

DroidEdit Termux integration

DroidEdit allows you to run commands if you have sl4a installed and the am command works wonderfully... I was wondering if there is a way to use am to launch an app with params .. specifically I want a command that will tell termux to run a command on a file that I specificy ... so far I found this snippet that launches the termux app .. now how do I get it to run node or webpack from npm as well?
am start --user 0 -n com.termux/com.termux.app.TermuxActivity

Cannot run my JMeter webdriver sampler script via command prompt

When I try to run my JMeter WebDriver sampler script via command prompt, the error below shows up. Is there any solution for this?
F:\apache-jmeter-3.2\bin\TestScriptRecorder.jmx is not a valid Win32 application.
It seems you are trying to execute .jmx file directly, it won't work this way, you need to launch jmeter.bat file and pass the .jmx file via -t command-line argument like:
F:\apache-jmeter-3.2\bin\jmeter.bat -n -t F:\apache-jmeter-3.2\bin\TestScriptRecorder.jmx -l result.jtl
or
java -jar F:\apache-jmeter-3.2\bin\ApacheJMeter.jar -n -t F:\apache-jmeter-3.2\bin\ -l result.jtl
References:
Non-GUI Mode (Command Line mode)
Full list of command-line options
How Do I Run JMeter in Non-GUI Mode?
If you want run your Jmx through command line follow the below steps.
Open command prompt
go to the path of where your jmeter.bat or jmete.sh file is placed(ex: C:\Users\ABC\apache-jmeter-3.3\bin)
Write the command as below
For Windows: jmeter -n -t C:\your_testScript_path\yourscript.jmx
For ubuntu/linux: ./jmeter.sh -n -t C:\your_testScript_path\yourscript.jmx
you can aslo pass any values to the jmx files using command line using command line paremeters like -JUsers=10 where in it should be defined in jmx like ${__P(Users,1)}

how to reload the nodejs app after making changes to the file?

i have a simple js file with http module to test Hello node for nodejs....
below is the http_test.js file
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200);
res.end('hello node');
}).listen(8080);
node http_test.js prints fine on the browser...now if i change my response line to say res.end('changed hello node to hello stoner');, i still get previous hello node on my page....
to get the changed line i got to end the current instance of node and then again run
node http_test.js
if i make any changes to js file should i restart over?
wouldnt just hitting refresh on my browser do it?
You need to stop and re run the server to see your latest update. To automate this, you can use nodemon, do npm i nodemon -g
And run nodemon http_test.js
Now, for every change you make tohttp_test.js the server will be restarted automatically
1) Install nodemon. To install, from your terminal run:
npm install -g nodemon
2) Now, go to terminal, where you have the program. And, run
nodemon http_test.js
Now, everytime when you make changes to your app, just save your changes and it will get reflected.
Details :-
Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Install it using npm.
Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
Please refer :-
http://nodemon.io/
https://github.com/remy/nodemon
You have to restart your server, because the file had load to the memory .
Or you can use nodemon who auto restart your server when you change file.

How to run html file on localhost?

I have an HTML file and I run it on localhost. But, this file includes a mirror using a webcam. For example, how can I run this HTML file on localhost? Webcam starts in this example when checking to live checkbox.
You can run your file in http-server.
1> Have Node.js installed in your system.
2> In CMD, run the command npm install http-server -g
3> Navigate to the specific path of your file folder in CMD and run the command http-server
4> Go to your browser and type localhost:8080. Your Application should run
You can use python -m http.server. By default the local server will run on port 8000. If you would like to change this, simply add the port number python -m http.server 1234
If you are using python 2 (instead of 3), the equivalent command is python -m SimpleHTTPServer
If you are running Python3, you may want to instead try:
python -m http.server
See this answer.
Install Node js - https://nodejs.org/en/
go to folder where you have html file:
In CMD, run the command to install http server- npm install http-server -g
To load file in the browser run - http-server
If you have specific html file. Run following command in CMD.- http-server fileName
by default port is 8080
Go to your browser and type localhost:8080. Your Application should
run there.
If you want to run on different port: http-server fileName -p 9000
Note : To run your .js file run: node fileName.js
If you have Node.js installed then from the folder you want to share you can simply run:
npx http-server
To add CORS you can run:
npx http-server --cors
On macOS:
Open Terminal (or iTerm) install Homebrew then run brew install live-server and run live-server.
You also can install Python 3 and run python3 -m http.server PORT.
On Windows:
If you have VS Code installed open it and install extension liveserver, then click Go Live in the bottom right corner.
Alternatively you can install WSL2 and follow the macOS steps via apt (sudo apt-get).
On Linux:
Open your favorite terminal emulator and follow the macOS steps via apt (sudo apt-get).
As Nora suggests, you can use the python simple server.
Navigate to the folder from which you want to serve your html page, then execute python -m SimpleHTTPServer.
Now you can use your web-browser and navigate to http://localhost:8000/ where your page is being served.
If your page is named index.html then the server automatically loads that for you. If you want to access any other page, you'll need to browse to http://localhost:8000/{your page name}
You can try installing one of the following localhost softwares:
xampp
wamp
ammps server
laragon
There are many more such softwares but the best among them are the ones mentioned above. they also allow domain names (for example: example.com)
You can install Xampp and run apache serve and place your file to www folder and access your file at localhost/{file name}
or simply at localhost if your file is named index.html
You can also use PHP to server the files in http
make sure you installed PHP, run the below command to verify
php --version
if PHP is not installed run below command to install it
sudo apt install php7.4-cli
Once after the installation go to the file path and execute the below command in the terminal
php -S localhost:8000
just npx serve it's more compatible with esmodule

Categories