简体   繁体   中英

How to run .php script on node.js

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,

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!");

You need to define the routing using Express methods app object that correspond to HTTP methods.

var express = require('express')
var app = express()

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
  res.send('hello world')
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM