php - node.js sending POST requests not working -
i'm trying send post request not sending. dont output in console log of browser.
my node server.js running in x.x.x.x:8000 connect client.html. x.x.x.x:8000/client.html
here node.js server.
function handler (req, res) { var filepath = '.' + req.url; if (filepath == './') filepath = './client.html'; var extname = path.extname(filepath); var contenttype = 'text/html'; switch(extname){ case '.js': contenttype = 'text/javascript'; break; case '.css': contenttype = 'text/css'; break; } path.exists(filepath, function (exists){ if (exists){ fs.readfile(filepath, function(error, content){ if(error){ res.writehead(500); res.end(); } else{ res.writehead(200, { 'content-type': contenttype }); res.end(content, 'utf-8'); } }); } else{ res.writehead(404); res.end(); } }); }
javascript code - i'm using ajax call , sending request command.php
$.post( '/var/www/command.php', { cmd: "out" }, function( data ){ console.log(data); });
php command.php - writes the named pipe in linux. when done writing echo success.
<?php if ($_post['cmd'] === 'out') { $con = fopen("/tmp/myfifo", "w"); fwrite($con, "out\n"); fclose($con); echo "success"; exit; } ?>
why not sending post requests command.php? there way me call command.php , execute commands in it?
because nodejs runs js, not php. also, unlike apache has built-in file handling, in nodejs, need build code or use existing library route urls files.
as question, it's either you:
setup server execute php. ajax calling nodejs server. route request nodejs php server (apache or whatever), making nodejs act proxy.
or create code in javascript for nodejs runs similar routine php script, , won't need php or server anymore.
Comments
Post a Comment