I'm using wamp server and node.js to run my app(server.js), but when I want to execute .php script I always got an error:
POST http://localhost:8080/login.php 404 (Not Found)
server.js
var app = require('express')();
var server = require('http').createServer(app);
var webRTC = require('webrtc.io').listen(server);
var exec = require("child_process").exec;
var port = process.env.PORT || 8080;
server.listen(port);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.get('/login.php', function(req, res){
exec("wget -q -O - http://localhost/login.php", function (error, stdout, stderr) {res.send(stdout);});});
in index.html calls to login.php:
$("#login").click(function(){
username=$("#user_name").val();
password=$("#password").val();
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
success: function(html)
{......
I want to ask, it's neccessary to install another tool or something else ?
thank you.
Node.js won't execute your PHP code, the Apache server will. As I understand your question you have an Apache server listening on port 80 and a Node.js server listening on 8080 and you want the HTML page served by Node.js to perform an Ajax post on the Apache served login.php. If this assertion is true then the problem is that your Ajax request point to localhost:8080 instead of localhost:80.
You must give an absolute URL to the Ajax request parameters to correctly point to the Apache server (port 80), giving a relative URL as you do it right now will perform the request to localhost:8080 which is your Node.js server.
So, replacing:
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
by
$.ajax({
type: "POST",
url: "http://localhost:80/login.php",
data: "name="+username+"&pwd="+password,
should do the trick.
You certainly want to get the server address from the actual page which you can do like this in JavaScript:
$.ajax({
type: "POST",
url: window.location.href.replace(/^(https?:\/\/[^\/]+/,'$1:80/') + "login.php",
data: "name="+username+"&pwd="+password,
Installation npm install node-php Usage To run wordpress with node js and express do this:
var express = require('express'); var php = require("php"); var path = require("path");
var app = express();
app.use("/", php.cgi("/path/to/wordpress"));
app.listen(9090);
console.log("Server listening!");
I do not understand it a lot. But I think there is no way to display or execute PHP in JS or HTML (check if you know do it classic way, try write PHP code into .html or .js). Save JS as PHP and give the code into.
<html><script>*YourScript*</script></html>
I think this could be the way it is running.