How to run .php script on node.js

11,376

Solution 1

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,

Solution 2

To install to Node.js:

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!");
Share:
11,376
Patrik18
Author by

Patrik18

Updated on June 05, 2022

Comments

  • Patrik18
    Patrik18 almost 2 years

    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.

  • Patrik18
    Patrik18 about 11 years
    actually, now I got an error: XMLHttpRequest cannot load http://localhost/login.php. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
  • malko
    malko about 11 years
    you have now to check about Access-Control policy headers developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS Your apache web server must return good headers to allow cross domain requests