简体   繁体   English

使用Node.js路由欺骗index.php

[英]Spoof index.php with Node.js route

I would like to rewrite my php app in node.js. 我想在node.js中重写我的php应用程序。 One of my problems is that we have some legacy client apps written in other languages that point directly to a php file. 我的一个问题是我们有一些用其他语言编写的遗留客户端应用程序直接指向php文件。 Is it possible to spoof a php file within an express route? 是否可以在快速路线中欺骗php文件?

I have tried the following: 我尝试过以下方法:

app.get('/index.php/', function(req, res){
    res.end('test');
});

but typing in {my domain}/index.php/ gives me 但输入{my domain} /index.php/给了我

Cannot GET /index.php 不能GET /index.php

What I'd love to have is a routes file called legacy.js, then over time as the legacy apps are updated then I can remove the routes one by one. 我喜欢的是一个名为legacy.js的路由文件,然后随着旧版应用程序的更新,我可以逐个删除路由。

Cheers for any help, 欢呼任何帮助,

Robin 知更鸟

Couple of suggestions 几个建议

Suggestion 1 建议1

You are getting a 404 from your route above due to the trailing slash in the route definition. 由于路径定义中的尾部斜杠,您从上面的路线获得404。 Change to: 改成:

app.get('/index.php', function (req, res, next) {
  res.send('PHP route called!');
});

Suggestion 2 建议2

Instead of trying to make Node handle your PHP file execution, why not setup nginx/apache as a reverse proxy for node? 为什么不将nginx / apache设置为节点的反向代理,而不是试图让Node处理你的PHP文件? For instance, with nginx , we can run PHP scripts and a node backend server simultaneously: 例如,使用nginx ,我们可以同时运行PHP脚本和节点后端服务器:

upstream node {
    server localhost:3000;
}

server {
    listen 8080;
    server_name localhost;

    root /path/to/root/directory;
    index index.php;

    # Here we list base paths we would like to direct to PHP with Fast CGI
    location ~* \/tmp|\/blog$ { {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    # Here we set a reverse proxy to upstream node app for all routes
    # that aren't filtered by the above location directives.
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://node;
        proxy_redirect off; 
    }
}

This allows you to run both PHP and node on the same domain and without the headache of forking child processes for each PHP script to execute — not to mention the memory impact this would have on your machine. 这允许您在同一个域上运行PHP和节点,而不必担心每个PHP脚本都要执行子进程 - 更不用说这会对您的计算机造成内存影响。

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

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