简体   繁体   English

如何在 node.js 上运行 .php 脚本

[英]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)我正在使用 wamp 服务器和 node.js 来运行我的应用程序(server.js),但是当我想执行 .php 脚本时,我总是收到一个错误: POST http://localhost:8080/login.php 404 (Not Found)

server.js服务器.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:index.html 中调用 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. Node.js 不会执行您的 PHP 代码,Apache 服务器会。 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.据我了解您的问题,您有一个 Apache 服务器侦听端口 80,一个 Node.js 服务器侦听 8080,并且您希望 Node.js 提供的 HTML 页面在 Apache 提供的 login.php 上执行 Ajax 发布。 If this assertion is true then the problem is that your Ajax request point to localhost:8080 instead of localhost:80.如果此断言为真,那么问题在于您的 Ajax 请求指向 localhost:8080 而不是 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.您必须为 Ajax 请求参数提供一个绝对 URL 以正确指向 Apache 服务器(端口 80),现在提供一个相对 URL 将执行对 localhost:8080 的请求,它是您的 Node.js 服务器。

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:您当然希望从实际页面获取服务器地址,您可以在 JavaScript 中这样做:

$.ajax({
  type: "POST",
  url: window.location.href.replace(/^(https?:\/\/[^\/]+/,'$1:80/') + "login.php",
  data: "name="+username+"&pwd="+password,

To install to Node.js:要安装到 Node.js:

npm install node-php

Usage用法

To run WordPress with Node.js and Express, do this:要使用 Node.js 和 Express 运行 WordPress,请执行以下操作:

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.您需要使用与 HTTP 方法相对应的 Express 方法应用程序对象来定义路由。

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')
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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