Javascript 使用Node.js路由欺骗index.php
我想在node.js中重写我的php应用程序。我的一个问题是,我们有一些用其他语言编写的遗留客户端应用程序直接指向php文件。是否有可能在express route中欺骗php文件 我尝试了以下方法:Javascript 使用Node.js路由欺骗index.php,javascript,php,node.js,express,Javascript,Php,Node.js,Express,我想在node.js中重写我的php应用程序。我的一个问题是,我们有一些用其他语言编写的遗留客户端应用程序直接指向php文件。是否有可能在express route中欺骗php文件 我尝试了以下方法: app.get('/index.php/', function(req, res){ res.end('test'); }); 但是在{mydomain}/index.php/中键入会给我 无法获取/index.php 我希望有一个名为legacy.js的路由文件,随着时间的推移,随着遗
app.get('/index.php/', function(req, res){
res.end('test');
});
但是在{mydomain}/index.php/中键入会给我
无法获取/index.php
我希望有一个名为legacy.js的路由文件,随着时间的推移,随着遗留应用程序的更新,我可以逐个删除路由
为任何帮助干杯
罗宾几点建议
建议1
由于路由定义中的尾随斜杠,您将从上面的路由中获得404。改为:
app.get('/index.php', function (req, res, next) {
res.send('PHP route called!');
});
建议2
与其让Node处理PHP文件执行,为什么不将nginx/apache设置为Node的反向代理?例如,使用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和node,而无需为每个PHP脚本执行分支子进程,更不用说这会对您的计算机造成内存影响