PHP exec command node - javascript

I'm running Apache and Node.js on the same server.
I tried to execute this command using PHP:
exec('usr/bin/node', 'var/www/html/app/node/server.js');
var_dump($output);
It returned:
"array(0) {}";
Do you have any idea why PHP did not execute the command node?
Thank you for your help.

Try absolute paths:
exec('/usr/bin/node /var/www/html/app/node/server.js', $output);
usr/bin/node is a relative path meaning it will appended to the current directory php script is executed in.

Related

Node.js source code getting sent over to client

I'm building with Node.js locally and I came along a weird problem where if I typed localhost:8080/server.js, the whole server-side source code shows up in the browser (the server file's name is server.js). I guess what's happening is the server looks for server.js, finds itself in the directory and sends it over. That's a huge security risk right? Any way to solve it?
All my code files are present in the same folder; would changing that be the best way to fix the problem?
Since index.html and server.js are in the same folder then the server will send server.js when requested because it thinks it's javascript code for the client and not the server.
You should put the files you want to serve in sub folder (usually named public) like this:
public/index.html
public/style.css
then start the server using that folder with:
app.use(express.static("public"))

Execute a shell command in nodejs that is passed inside a variable

I have setup a nodejs server on a machine where I would like to execute scripts. The command is created on the client side and is sent to the nodejs server through a variable. This command contains a bunch of spaces, along with the script and the arguments needed for it to be executed.
An example command to be executed on the machine: <script.sh> -input <inputfile> -output <outputDir> -helper_file <file>
var child = exec('$command')
This command is passed to the server inside a variable. When I try to run the command with the exec process, it stumbles on the spaces and the arguments. How do I get around this issue?
Thanks

how to make a node js run a js file and issues creating a js file in node.js console window with win10

I am a beginner in node js and have a couple of months experience with javascript, have finished codecademy js and jquery introductory courses and learned more about js in the internet and have an understanding of html and css in intermediate level or near to intermediate level. I am a complete noob in getting the js and node js to run alltogether. Using VS code for text editor. Node.js is installed from their original homepage, using node.js APP. File named global js.is sticked to 2 folders: the same with node.js and desktop folder for vs code files( the ones I create in VS code as projects or just simple files). The darn thing just doesn't make sense to me and I dont get this mess to work either. To be more specific, then:
I have 2 issues:
Firstly, did an install: npm install javascript bla bla. It was a sucsess, mkdir and cd were successful. NUL >introduction.js says access denied.
Alos I have a file named global.js. It is sitting in the same foler as node.js + the original version of this fie is in the same folder of desktop vs code's excercises. I can't make my node.js open this global.js file.
How do I overcome of these issues?
I tried to use this as a guide line: How to run a hello.js file in Node.js on windows?.
Did following parts of that above: changed account type to ADMINISTRATOR via appwize smth from run. Called cmd from run after having given myself and admin status permanently and windows logged me off and back on again. Tried to run the file from command prompt, declaring the exact path to file in cmd. It reached to the path, showed no error, went to the file from there. No errors, NOTHING didn't happen... Tried the global install whatever faced an issue in there and got stuck with it.
I need some help in here! Would be nice if someone could explain to me what is wrong and what's the basic concept of using node.js or what are the alternative ways to programmetely launch it?
Do I need to use the node.js console or node.js app?
CODE IN main. js :
var http = require("http");
var path= require ("path");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
console.log (`Rock on World from ${path.basename(__filename)}`);
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
No need to use any special console, on windows, standard cmd is enough.
Check if node is installed properly first. Run node -v and see if it prints its version.
Create a folder for your project and make sure your are in it before executing node or npm. Node.exe doesn't have to be in the project folder as long as it is in the path (by default it is)
Make sure your source files in that project folder. (test.js, introduction.js .. whatever you will run)

Node cron not running on EC2

I have python and node scripts set as cron jobs on EC2. The python scripts run but the node scripts do not run. The node scripts run fine when activated manually from the command line. This is a simple script that finds out if today is a holiday and then puts the boolian answer into a file named isHoliday.csv.
What I've tried so far:
Set as crontab using: crontab -e (last entry in this pic)
Set as crontab using: sudo crontab -e
Created a strange cron.d file, put the cron details in it, and chmod'd it 0644, using: sudo crontab -e; shown above.
cron.d contents:
I see the log file is named holiday_script.log in the sudo cron, and holiday.log in the cron.d file, but neither log file has been created.
Replaced relative paths with absolute paths. (But I think this is still the most likely culprit.)
Code in the holiday.js file:
// Check if it is a holiday, t/f
var holidayChecker = require('usa-holidays');
var date = new Date();
var holidayObj = holidayChecker.make(date);
var isHoliday = !holidayObj.isBusinessDay();
// put the answer into a file so it can be read by python crons
var fs = require('fs');
fs.writeFile("/home/ec2-user/stack/isHoliday.csv", isHoliday, function(err) {
if(err) {
return console.log(err);
}
});
I don't know the exact solution, but you can view your cron logs by moving to a different directory. When you first login to EC2, do this twice: cd ..
That will take you up two directories. Then you'll need to switch user to sudo like this: sudo su
Then move to the var/log folder. There should be a file named "cron". Since you have crons setup as *****, that file should be filling up with data every second. Tail it by typing: tail -f cron. That will allow you to see the crons execute in real time.
Also, the "mail" notice: navigate to that folder and use less to read the mail. That file will have more clues. Scroll down with the down-arrow.
That isn't the answer but it will help you move forward a bit.
If you just want to run JS/TS code at a scheduled time interval without worrying about cron-jobs or servers, you can check out services like https://chunk.run

How to render Jade code on browser?

I'm new to Jade and I want to know How to run the .jade file , I'm using Node.js and i installed the jade node module .. what's the next step to run the code and find it in the browser ?
1) You must run your Node.js server on the command line.
$ node app.js
2) Go to your browser and use the port number that your server is running on. Generally you want to console.log the port number in your app.js file so the port is displayed on the command line.
localhost:3000
3) https://github.com/visionmedia/jade for examples and more documentation to get you off the ground.

Categories

Resources