How to run .php script on node.js
node.js, php
Solution
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,
Problem
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.