用Node.js路由欺骗index.php

我想在node.js中重写我的php应用程序。 我的问题之一是,我们有一些用其他语言编写的旧版客户端应用程序直接指向一个php文件。 是否有可能欺骗一个快速路线的PHP文件?

我已经尝试了以下内容:

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

但在{我的域名} /index.php/input给我

无法获取/index.php

我喜欢的是一个名为legacy.js的路由文件,随着旧版应用程序的更新,我可以逐一删除路由。

欢呼任何帮助,

知更鸟

几个build议

build议1

由于path定义中的尾部斜线,您将从上面的路线中获取404。 改成:

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

build议2

而不是试图让节点处理您的PHP文件执行,为什么不设置nginx / apache作为节点的反向代理? 例如,用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; } } 

这允许您在同一个域上运行PHP和节点,并且不必为每个PHP脚本执行分叉subprocess而头疼 – 更不用说这会影响到您的计算机。