pinug pinug - 3 months ago 55
Javascript Question

execute node with php

i want to execute a javascript file with node(node.js) without using windows terminal, so i used php's exec() function to execute the file, but it didn't work.
What is the problem ? Did i write the wrong windows command ? Did i write wrong full path of node? (i wrote it but maybe it's a backslash problem)? Windows don't know the "node" command ?



console.log('Welcome to Node.js !');

<?php
echo exec('C:\Program Files (x86)\nodejs\node.exe welcometonode.js');//not working
echo exec('node welcometonode.js');//not working
echo exec('C:\\Program Files (x86)\\nodejs\\node.exe welcometonode.js');//not working
echo exec('C:\Program Files (x86)\nodejs\node welcometonode.js');//not working



?>




Answer

Windows does not work well with spaces as you need to tell it it's all part of the same command, you will need to enclose the full command path within double quotes (singles don't work), then replace your backward slashes for forward slashes, is always better to use forward within your scripts, also, provide the full path to the node script as well, else it will assume the script exists on whatever directory your php script or php binary itself is running, so is better to avoid confusions, this would work:

exec("\"C:/Program Files (x86)/nodejs/node.exe\" \"C:/path/to/script/welcometonode.js\"");